note 69485 added to function.fgetcsv

From: Date: Thu, 07 Sep 2006 13:23:21 +0000
Subject: note 69485 added to function.fgetcsv
Groups: php.notes 
Request: Send a blank email to php-notes+get-116825@lists.php.net to get a copy of this message
I needed a fast/robust csv parser in PHP that could handle unix-, windows- and mac-style linebreaks. i takes a csv-string as input and outputs a multidimensional array with lines and fields. <?php function parse_csv_php(&$data,$delim=',',$enclosure='"') { $enclosed=false; $fldcount=0; $linecount=0; $fldval=''; for($i=0;$i<strlen($data);$i++) { $chr=$data{$i}; switch($chr) { case $enclosure: if($enclosed&&$data{$i+1}==$enclosure) { $fldval.=$chr; ++$i; //skip next char } else $enclosed=!$enclosed; break; case $delim: if(!$enclosed) { $ret_array[$linecount][$fldcount++]=$fldval; $fldval=''; } else $fldval.=$chr; break; case "\\r": if(!$enclosed&&$data{$i+1}=="\\n") continue; case "\\n": if(!$enclosed) { $ret_array[$linecount++][$fldcount]=$fldval; $fldcount=0; $fldval=''; } else $fldval.=$chr; break; default: $fldval.=$chr; } } if($fldval) $ret_array[$linecount][$fldcount]=$fldval; return $ret_array; } ?> ---- Server IP: 213.206.121.69 Probable Submitter: 212.125.128.171 ---- Manual Page -- http://www.php.net/manual/en/function.fgetcsv.php Edit -- https://master.php.net/note/edit/69485 Del: integrated -- https://master.php.net/note/delete/69485/integrated Del: useless -- https://master.php.net/note/delete/69485/useless Del: bad code -- https://master.php.net/note/delete/69485/bad+code Del: spam -- https://master.php.net/note/delete/69485/spam Del: non-english -- https://master.php.net/note/delete/69485/non-english Del: in docs -- https://master.php.net/note/delete/69485/in+docs Del: other reasons-- https://master.php.net/note/delete/69485 Reject -- https://master.php.net/note/reject/69485 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#116825) next »