0

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

1

You could use

fwrite('text.txt', str_pad('12345', 10, " "));
fwrite('text.txt', str_pad('1234', 5, " "));
fwrite('text.txt', str_pad('12346789123', 12, " "));
Mark B
  • 649
  • 5
  • 11