Write to TXT file with length delimited data

626 views Asked by At

I'm trying to write to a text file on php, the fields need to be a certain length for each one. Order Number is 10 characters long, order name is 5 characters, telephone is 12 characters long.

Ex:

12345    1234 123456789123

If I try:

fwrite('text.txt', '12345', 10);
fwrite('text.txt', '1234', 5);
fwrite('text.txt', '12346789123', 12);

I get: 123451234123456789123

How can I tell it to add the spaces to the specific size I need in each field.

1

There are 1 answers

0
Mark B On

You could use

fwrite('text.txt', str_pad('12345', 10, " "));
fwrite('text.txt', str_pad('1234', 5, " "));
fwrite('text.txt', str_pad('12346789123', 12, " "));