note 107457 added to function.str-getcsv
| From: | (at) | Date: | Fri, 10 Feb 2012 14:07:03 +0000 |
| Subject: | note 107457 added to function.str-getcsv | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-186051@lists.php.net to get a copy of this message | ||
Here's the function made by silaspalmer at gmail dot com modified to output an associative
array.
function parse_csv($file,$comma=',',$quote='"',$newline="\n")
{
$db_quote = $quote . $quote;
// Clean up file
$file = trim($file);
$file = str_replace("\r\n",$newline,$file);
$file = str_replace($db_quote,'"',$file); // replace double quotes with
" HTML entities
$file = str_replace(',",',',,',$file); // handle ,"",
empty cells correctly
$file .= $comma; // Put a comma on the end, so we parse last cell
$inquotes = false;
$start_point = 0;
$row = 0;
$cellNo = 0;
for($i=0; $i<strlen($file); $i++) {
$char = $file[$i];
if ($char == $quote) {
if ($inquotes) {
$inquotes = false;
}
else {
$inquotes = true;
}
}
if (($char == $comma or $char == $newline) and !$inquotes) {
$cell = substr($file,$start_point,$i-$start_point);
$cell = str_replace($quote,'',$cell); // Remove delimiter quotes
$cell = str_replace('"',$quote,$cell); // Add in data quotes
if ($row>0) $data[$row][$data[0][$cellNo]] = $cell;
else $data[$row][] = $cell;
$cellNo++;
$start_point = $i + 1;
if ($char == $newline) {
$cellNo = 0;
$row ++;
}
}
}
return $data;
}
----
Server IP: 69.147.83.197
Probable Submitter: 95.76.157.240
----
Manual Page -- http://www.php.net/manual/en/function.str-getcsv.php
Edit -- https://master.php.net/note/edit/107457
Del: integrated -- https://master.php.net/note/delete/107457/integrated
Del: useless -- https://master.php.net/note/delete/107457/useless
Del: bad code -- https://master.php.net/note/delete/107457/bad+code
Del: spam -- https://master.php.net/note/delete/107457/spam
Del: non-english -- https://master.php.net/note/delete/107457/non-english
Del: in docs -- https://master.php.net/note/delete/107457/in+docs
Del: other reasons-- https://master.php.net/note/delete/107457
Reject -- https://master.php.net/note/reject/107457
Search -- https://master.php.net/manage/user-notes.php