How to write a fixed width text file using PHP

I am trying to do something that should be easy, but with problems.

All I want to do is generate a report from a MySQL table and have it as a simple .txt file. I need a fixed file width, so everything is lined up and looks good. I'm going to use a courier mail font and it will just be a barebone table, but how can I get everything to line up, like a fixed-width file?

Do I need to create an array of values โ€‹โ€‹filled with spaces to get them the same width? or is there a php function for writing at a specific position?

Thanks again for your help!

+6
php
source share
1 answer

Take a look at the str_pad method. For example.

 str_pad('Somevalue', 20, " ", STR_PAD_LEFT); 

It remains to leave the string "Somevalue" with spaces, specifying

 " Somevalue" 

While

 str_pad('Somevalue', 20); 

Insert the right line into spaces, specifying:

  "Somevalue " 
+9
source share

All Articles