note 54555 deleted from function.explode by bjori
| From: | bjori@php.net | Date: | Mon, 25 Sep 2006 08:08:54 +0000 |
| Subject: | note 54555 deleted from function.explode by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-117581@lists.php.net to get a copy of this message | ||
Note Submitter: AussieDan
----
Here is an expanded version of the csv_explode function which can parse a string consisting of
multiple csv rows.
<?php
function csv_parse($str,$f_delim = ',',$r_delim = "\n",$qual =
'"')
{
$output = array();
$row = array();
$word = '';
$len = strlen($str);
$inside = false;
$skipchars = array($qual,'\\');
for ($i = 0; $i < $len; ++$i) {
$c = $str[$i];
if (!$inside && $c == $f_delim) {
$row[] = $word;
$word = '';
} elseif (!$inside && $c == $r_delim) {
$row[] = $word;
$word = '';
$output[] = $row;
$row = array();
} else if ($inside && in_array($c,$skipchars) && ($i+1 < $len &&
$str[$i+1] == $qual)) {
$word .= $qual;
++$i;
} else if ($c == $qual) {
$inside = !$inside;
} else {
$word .= $c;
}
}
$row[] = $word;
$output[] = $row;
return $output;
}
?>