Actually, this version is better than the last I submitted. The goal here is to be able to engage in *multiple* delimeter removal passes; for all but the last pass, set the third value to "1", and everything should go well.
function quotesplit( $splitter=',', $s, $restore_quotes=0 )
{
//First step is to split it up into the bits that are surrounded by quotes
//and the bits that aren't. Adding the delimiter to the ends simplifies
//the logic further down
$getstrings = explode('"', $splitter.$s.$splitter);
//$instring toggles so we know if we are in a quoted string or not
$delimlen = strlen($splitter);
$instring = 0;
while (list($arg, $val) = each($getstrings))
{
if ($instring==1)
{
if( $restore_quotes )
{
//Add the whole string, untouched to the previous value in the array
$result[count($result)-1] = $result[count($result)-1].'"'.$val.'"';
} else {
//Add the whole string, untouched to the array
$result[] = $val;
}
$instring = 0;
}
else
{
//Break up the string according to the delimiter character
//Each string has extraneous delimiters around it (inc the ones we added
//above), so they need to be stripped off
$temparray = split($splitter, substr($val, $delimlen, strlen($val)-$delimlen-$delimlen+1 ) );
while(list($iarg, $ival) = each($temparray))
{
$result[] = trim($ival);
}
$instring = 1;
}
}
return $result;
}