note 53932 added to function.fgetcsv
| From: | cristianDOTzuddasATgmailDOTcom at osu1 dot php dot net | Date: | Fri, 17 Jun 2005 13:58:25 +0000 |
| Subject: | note 53932 added to function.fgetcsv | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-90584@lists.php.net to get a copy of this message | ||
"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;
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.fgetcsv.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+53932
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+53932&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+53932&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+53932&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+53932&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+53932&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+53932&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+53932&report=yes
Search -- http://master.php.net/manage/user-notes.php