Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write fixed width text file with PHP

Tags:

php

I'm trying to do something that should be easy, but having problems.

All I want to do is generate a report from a MySQL table, and have it as a plain .txt file. I need the file fixed width, so everything lines up and looks good. I'm going to use the courier font, and it's just going to be a barebones table, but how do I actually get everything to line up, the way a fixed-width file should?

Do I have to build up an array of values padded with spaces to get them the same width? or is there a php function to begin writing at a specific position?

Thanks again for the help!

like image 451
Reg H Avatar asked Feb 19 '11 12:02

Reg H


1 Answers

Take a look at the str_pad method. E.g.

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

Will left-pad the string "Somevalue" with spaces, giving

"           Somevalue"

Whereas

str_pad('Somevalue', 20);

Will right-pad the string with spaces, giving:

 "Somevalue           "
like image 191
Klaus Byskov Pedersen Avatar answered Sep 29 '22 09:09

Klaus Byskov Pedersen