Re: preserving blank space padding when using fgetcsv

From: Date: Thu, 04 Dec 2003 10:54:15 +0000
Subject: Re: preserving blank space padding when using fgetcsv
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-171795@lists.php.net to get a copy of this message
In article <3FCE3429.7000606@bgnet.bgsu.edu>, roger@bgnet.bgsu.edu says... > 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. If you want to see multiple white space in the browser, you'll need to surround the text with <PRE> tags. Browsers tend to ignore white space (multiple spaces, tabs, EOL etc). If you do a view source of your current output you will see the spaces. Cheers -- Quod subigo farinam A: Because it messes up the order in which people normally read text. Q: Why is top-posting such a bad thing? A: Top-posting. Q: What is the most annoying thing on usenet?

« previous php.general (#171795) next »