note 37004 added to function.explode
| From: | ng4rrjanbiah at rediffmail dot com | Date: | Thu, 30 Oct 2003 12:03:12 +0000 |
| Subject: | note 37004 added to function.explode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-59522@lists.php.net to get a copy of this message | ||
The improved CSV Explode function of "hhromic at udec dot cl" again has one limitation
with MS Excel.
His version returns "" for """". But the expected result is "
for """"
And so, I have modified his code with additional inside check.
<?php
//Explode CSV (MS Excel) string
function csv_explode($str, $delim = ',', $qual = "\"")
{
$len = strlen($str);
$inside = false;
$word = '';
for ($i = 0; $i < $len; ++$i) {
if ($str[$i]==$delim && !$inside) {
$out[] = $word;
$word = '';
} else if ($inside && $str[$i]==$qual && ($i<$len &&
$str[$i+1]==$qual)) {
$word .= $qual;
++$i;
} else if ($str[$i] == $qual) {
$inside = !$inside;
} else {
$word .= $str[$i];
}
}
$out[] = $word;
return $out;
} /*-------csv_explode()------------*/
//Test...
$csv_str = 'a,"""","""",d,e,f';
print_r( csv_explode($csv_str) );
?>
HTH,
R. Rajesh Jeba Anbiah
----
Manual Page -- http://www.php.net/manual/en/function.explode.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+37004
Delete -- http://master.php.net/manage/user-notes.php?action=delete+37004&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+37004&report=yes
Search -- http://master.php.net/manage/user-notes.php