kreamweb
5/14/2012 - 1:21 PM

csv to array

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; 
}