Convert CSV data into fixed width records using PHP and php-excel-reader class

2.1k views Asked by At

My end result is to have a fixed length record created from either a csv of excel file using PHP. I have am working with excel_reader2.php and seems to import this nicely and displays it in the browser. I would like in turn to export or write the contents to a file so that each column would export as 35 characters fixed length fields . currently the $data is formatted as html and dont know how to write down what i am describing. Thank you in advance for someone to point me in the correct direction.

1

There are 1 answers

3
Keenora Fluffball On

You would take every row of the CSV file. Then, every column of the row. For every cell you could use:

$newcell = sprintf("[%35s]\n", $cell);

So you would have a 35 chars width string with the data of $cell. And then, just add it via two loops into a file.