note 50186 deleted from function.fgetcsv by danbrown
| From: | danbrown@php.net | Date: | Sun, 17 May 2009 18:17:34 +0000 |
| Subject: | note 50186 deleted from function.fgetcsv by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-155096@lists.php.net to get a copy of this message | ||
Note Submitter: marc at henklein dot com
----
for excel-csv-sheets like:
test1;test2;test3;test4;"test5
test5b
test5c";test6
(optionally enclosed by " including line-breaks)
.. i wrote a little function to solve this problem.
<?php
function CSV2Array($content, $delim = ';', $encl = '"', $optional = 1)
{
$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++;
}
return $liste;
}
// usage for example:
$content = join('',file('test.csv'));
$liste = CSV2Array($content);
print_r($liste);
?>