Skip to content

Instantly share code, notes, and snippets.

@kreamweb
Created May 14, 2012 13:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kreamweb/2693947 to your computer and use it in GitHub Desktop.
Save kreamweb/2693947 to your computer and use it in GitHub Desktop.
csv to array
<?php
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][] = str_replace("\"","",$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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment