note 56968 added to function.fgetcsv

From: Date: Tue, 20 Sep 2005 15:09:24 +0000
Subject: note 56968 added to function.fgetcsv
Groups: php.notes 
Request: Send a blank email to php-notes+get-95484@lists.php.net to get a copy of this message
"cristian DOT zuddas AT gmail DOT com" has posted the corrected function "CSV2Array", but all the same there's an error. For example, function incorrectly processes a simple csv-file from "MS Excel" (http://wsCat.ka.pp.ru/test.csv) in which fields any way contain quotes and CR/LF . I has written working function which has successfully applied to import of the greater production catalog. <?php // --------- Test --------------------------------------- $str=file_get_contents('http://wsCat.ka.pp.ru/test.csv'); $rows=CSV2Array($str); echo '<table border="1" cellSpacing="2" cellPadding="2">'; for($i=0;$i<count($rows);$i++) { echo '<tr>'; for ($j=0;$j<count($rows[$i]);$j++) { echo '<td>'.$rows[$i][$j]. '</td>'; } echo '</tr>'; } echo '</table>'; // ----------------------------------------------------- /** *function CSV2Array *Convert CSV-text to 2d-array *(delimeter = ';', line ending = '\r\n' - only for windows platform) * @param string $query (query to database) * @return array */ function CSV2Array($content) { if ($content{strlen($content)-1}!="\r" && $content{strlen($content)-1}!="\n") $content .= "\r\n"; $arr=array(); $temp=$content; $tma=array(); while (strlen($temp)>0) { if ($temp{0}=='"') { $temp=substr($temp,1); $str=''; while (1) { $matches=array(); if (!preg_match('/^(.*?)"("*?)(;|\r\n)(.*)$/is',$temp,$matches)) return $arr; $temp=$matches[4]; if (fmod(strlen($matches[2]),2)>0) { $str.=$matches[1].$matches[2].'"'.$matches[3]; continue; } else { $tma[]=preg_replace('/""/','"',$str.$matches[1].$matches[2]); if ($matches[3]!=';') { $arr[]=$tma; $tma=array(); } break; } } } else { $matches=array(); if (!preg_match('/^([^;\r\n]*)(;|\r\n)(.*)$/is',$temp,$matches)) return $arr; $tma[]=$matches[1]; $temp=$matches[3]; if ($matches[2]!=';') { $arr[]=$tma; $tma=array(); } } } return $arr; } ?> ---- Manual Page -- http://www.php.net/manual/en/function.fgetcsv.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+56968 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56968&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56968&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56968&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56968&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56968&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+56968&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56968&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+56968&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#95484) next »