Re: Parsing CSV files

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




On Apr 28, 2007, at 2:59 PM, Richard Lynch wrote:

On Fri, April 27, 2007 5:25 pm, Fernando Cosso wrote:
One word: explode
:D

Two Words: Quotes, Commas
:-p


I think I wrote a "parser" a while back that properly handled commas in quotes for csv files. Probably did it the hard way but it worked for what I needed it to do, and it required less brain power on my part then trying to figure out a regex.

Went through the file a character at a time and ran the strings into arrays, ala explode(). Something along the lines of:

$out = array();
$line = 1;
$i = 0;

start of line add chars to $out[$line][$i] (+=) until you get to a comma ($i++), keep track of opening and closing quotes and ignore the commas in between those, then on to the next line ($line++, $i=0;).


My files were fairly small, so I'm not sure if it would be appropriate for large files or busy servers.


Ed

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux