Padding Variables?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Padding Variables?

Post by JPlush76 »

Hey guys, I have to pass data to another system and instead of comma separated I have to pass fixed width fields

I have to pass order data as a string
IE:

ordernum, customer_number, customer_name

well in my order file the ordernum might be 6 length in the mysql table, the customer_number 7 length, and the customer_name 30 length.

basically what I want to do is test the variable against the field length in the table and then if its less than that, pad the rest of the field with spaces

so my string would look like:

12 6985 John Smith

instead of:
126985JohnSmith

anyone? :(
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

you could do this:

Code: Select all

str_pad($var, 10, " ", STR_PAD_RIGHT);
now, $var would be the var getting padded, it will then pad it with " " until it is 10 chars in length, and it's set to pad to the right

remember, you're probably going to want to convert these " "s to &nbps;'s
Last edited by hob_goblin on Thu Oct 10, 2002 2:56 pm, edited 1 time in total.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

to retrieve the table-definition take a look at the result of

Code: Select all

$query = 'DESCRIBE '.$tablename;
(if you wanted to know this either ;) )
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

hob you are the F'ing man :)

works likes charm
Post Reply