Re: Parsing CSV files

From: Date: Sun, 29 Apr 2007 03:55:10 +0000
Subject: Re: Parsing CSV files
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-254102@lists.php.net to get a copy of this message
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

« previous php.general (#254102) next »