note 53932 deleted from function.fgetcsv by danbrown
| From: | danbrown@php.net | Date: | Sun, 17 May 2009 18:17:13 +0000 |
| Subject: | note 53932 deleted from function.fgetcsv by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-155094@lists.php.net to get a copy of this message | ||
Note Submitter: cristian DOT zuddas AT gmail DOT com
----
"marc at henklein dot com" posted a cool function "CSV2Array", but there's
an error. If the last char of the CSV file isn't a carriage return, the function can't
take the last field of the last line in the CSV.
Fixed adding this check:
<?
if ($content[strlen($content)-1]!="\r" &&
$content[strlen($content)-1]!="\n")
$content .= "\r\n";
?>
The updated function:
<?
function CSV2Array($content, $delim = ';', $encl = '"', $optional = 1) {
if ($content[strlen($content)-1]!="\r" &&
$content[strlen($content)-1]!="\n")
$content .= "\r\n";
$reg = '/(('.$encl.')'.($optional?'?(?(2)':'(').
'[^'.$encl.']*'.$encl.'|[^'.$delim.'\r\n]*))('.$delim.
'|\r\n)/smi';
preg_match_all($reg, $content, $treffer);
$linecount = 0;
for ($i = 0; $i<=count($treffer[3]);$i++) {
$liste[$linecount][] = $treffer[1][$i];
if ($treffer[3][$i] != $delim)
$linecount++;
}
unset($linecount);
unset($i);
unset($reg);
unset($content);
unset($delim);
unset($encl);
unset($optional);
unset($treffer);
return $liste;
}
?>