note 86880 deleted from function.fgetcsv by danbrown

From: Date: Fri, 07 Nov 2008 17:43:46 +0000
Subject: note 86880 deleted from function.fgetcsv by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-146293@lists.php.net to get a copy of this message
Note Submitter: earnest (dot) berry at gmail (dot) com ---- A lot of the other solutions forget about the escaping of commas and string delimitation. A simple "explode" will not work on a true CSV line. Here's a simple function I am using to parse CSV lines. Should get others started if they want to add more options to the function and/or make it more complex: <?php function my_parse_csv_str($line) { $info = array(); $ignore = FALSE; $delimiter = ","; $ci = ""; for($i=0;$i <= strlen($line);$i++) { $c = $line[$i]; if(($c == $delimiter || $c == "") && !$ignore) { $info[] = $ci; $ci = ""; } else if($c == '"') { $ignore = !$ignore; } else { $ci .= $c; } } return $info; } ?> This will parse csv lines like: Jim, Joe,"Mary, Sue, and Bill" into: <?php array( [0] => Jim, [1] => Joe, [2] => Mary, Sue, and Bill, ); ?> instead of: <?php array( [0] => Jim, [1] => Joe, [2] => Mary [3] => Sue [4] => and Bill ); ?>

« previous php.notes (#146293) next »