preserving blank space padding when using fgetcsv

From: Roger Spears (roger_at_bgnet.bgsu.edu)
Date: 12/03/03


Date: Wed, 03 Dec 2003 16:00:05 -0500
To: PHP - General <php-general@lists.php.net>

The first time I sent this message to the list, this is what bounced back...

"Hi. This is the qmail-send program at pb1.pair.com.
I'm afraid I wasn't able to deliver your message to the following addresses.
This is a permanent error; I've given up. Sorry it didn't work out.

<php-general@lists.php.net>:
This message is looping: it already has my Delivered-To line. (#5.4.6)"

So I am resending it. I apologize if anyone is getting this message twice......

Hello,

I've been researching this for most of the day and am unable to find an
answer.

I'm using fgetcsv to read a comma delimited file (Microsoft Excel CSV).
I'm trying to create a PHP application which will read a csv file line
by line, remove the commas and preserve the padding that is in each
column. That means if a column is a fixed length of 15 characters and
the actual text in that column is only 5 characters long, the
application will preserve the extra 10 blank spaces.

I'm able to open and read the csv using fgetcsv:

$handle = fopen ($filename, "r");

while ($mpt_line = fgetcsv ($handle, filesize ($filename), ","))

{

Then I check each member of that array to make sure it is the proper
length for that column:

if (strlen($mpt_line[0])<4)

    {

    str_pad($mpt_line[0], 4, " ", STR_PAD_RIGHT);

    }

When all of the length checks are finished I remove the commas and then
print out the finished product:

}

$no_commas = str_replace(",", "", $mpt_line);

print "aa/".$no_commas[0]."/aa";

For output all I get is the actual text and no blank space padding.
Meaning that if the field contains two characters of data it should
still appear 4 characters in length. I was hoping to achieve aa/hi /aa
instead all I get is aa/hi/aa.

Thanks,

Roger



Relevant Pages

  • Re: preserving blank space padding when using fgetcsv
    ... > I'm using fgetcsv to read a comma delimited file. ... > the actual text in that column is only 5 characters long, ... > When all of the length checks are finished I remove the commas and then ...
    (php.general)
  • RE: Getting around 255 Column limit
    ... Once I do write the CSV to a temp file, how do I then start a Word ... not format except the commas and returns. ... If OutputLine = "" Then ... to get my data fields from Excel into Word ...
    (microsoft.public.excel.programming)
  • Re: Excel text export limit - 1024 per line (not cell), workaround
    ... I am saving it as CSV - specifically what I am doing is taking a large ... today I had one field that had 690 characters in it. ... exporting the data and the version of excel you're using. ...
    (microsoft.public.excel.misc)
  • Re: Excel e-mail list to netscape
    ... If you have other characters interferring, you can use Replace to remove them. ... If you have unwanted commas at the end, ... Use Ctrl+End to see where Excel thinks the data ends. ... with a space) the macro would help, and link to how to install and use a macro can be found at the top of the join.htm page ...
    (microsoft.public.excel.misc)
  • Re: quote-free CSV exports?
    ... advent of databases storing more and more, the need came to have commas ... whether or not they are 'true csv files'? ... headers, without quotes, with a row of field names as the first ... Because of all this variation CSV is a crappy format to have to use. ...
    (comp.databases.filemaker)