In response to the getCSVValues() function posted by justin at cam dot org, my testing indicates that it has a problem with a CSV string like this:
1,2,"3,""4,5"",6",7
This parses as:
array(6) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
[2]=>
string(4) "3,"4"
[3]=>
string(1) "5"
[4]=>
string(0) ""
[5]=>
string(1) "7"
}
instead of:
array(4) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
[2]=>
string(9) "3,"4,5",6"
[3]=>
string(1) "7"
}
To fix this, I changed the second substr_count to look for an odd number of quotes, as opposed to any quotes at all:
<?php
function getCSVValues($string, $separator=",")
{
$elements = explode($separator, $string);
for ($i = 0; $i < count($elements); $i++) {
$nquotes = substr_count($elements[$i], '"');
if ($nquotes %2 == 1) {
for ($j = $i+1; $j < count($elements); $j++) {
if (substr_count($elements[$j], '"') %2 == 1) { array_splice($elements, $i, $j-$i+1,
implode($separator, array_slice($elements, $i, $j-$i+1)));
break;
}
}
}
if ($nquotes > 0) {
$qstr =& $elements[$i];
$qstr = substr_replace($qstr, '', strpos($qstr, '"'), 1);
$qstr = substr_replace($qstr, '', strrpos($qstr, '"'), 1);
$qstr = str_replace('""', '"', $qstr);
}
}
return $elements;
}
?>