note 94022 deleted from function.str-getcsv by googleguy
| From: | googleguy@php.net | Date: | Tue, 22 Oct 2013 11:17:33 +0000 |
| Subject: | note 94022 deleted from function.str-getcsv by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-197081@lists.php.net to get a copy of this message | ||
Note Submitter: Jrg Wagner
----
Here is a very concise replacement for str_getcsv.
No escaping of the enclosure char though, but an additional possibility to preserve the enclosing
characters around a field.
Note that the fourth parameter therefore has a different meaning!
<?php
function csv_explode($delim=',', $str, $enclose='"', $preserve=false){
$resArr = array();
$n = 0;
$expEncArr = explode($enclose, $str);
foreach($expEncArr as $EncItem){
if($n++%2){
array_push($resArr, array_pop($resArr) . ($preserve?$enclose:'') .
$EncItem.($preserve?$enclose:''));
}else{
$expDelArr = explode($delim, $EncItem);
array_push($resArr, array_pop($resArr) . array_shift($expDelArr));
$resArr = array_merge($resArr, $expDelArr);
}
}
return $resArr;
}
?>