note 87729 added to function.fgetcsv

From: Date: Thu, 18 Dec 2008 14:32:37 +0000
Subject: note 87729 added to function.fgetcsv
Groups: php.notes 
Request: Send a blank email to php-notes+get-148310@lists.php.net to get a copy of this message
Here is a script that convert a CSV to an double-array ([row][col]), none of the ones below works in my case. (Multi-line, quoted or not, ...) It use coma as field separator, and double-quote. $fileContent = file_get_contents("my_file.csv"); //Convert Windows CR to Linux $fileContent = str_replace("\r\n","\n",$fileContent); //Split file by finding right CR. $expr="/\n(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/"; $rows=preg_split($expr,trim($fileContent)); unset($fileContent); //Free up some memory $data = array(); foreach($rows as $row){ //Find right coma $expr="/,(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/"; $results = preg_split($expr,trim($row)); //Remove quote $data[] = preg_replace("/\"(.*)\"/s","$1",$results); } unset($rows); //Free up some memory //Show the result echo "<pre>"; print_r($data); echo "</pre>"; ---- Server IP: 195.207.10.20 Probable Submitter: 194.78.219.251 ---- Manual Page -- http://www.php.net/manual/en/function.fgetcsv.php Edit -- https://master.php.net/note/edit/87729 Del: integrated -- https://master.php.net/note/delete/87729/integrated Del: useless -- https://master.php.net/note/delete/87729/useless Del: bad code -- https://master.php.net/note/delete/87729/bad+code Del: spam -- https://master.php.net/note/delete/87729/spam Del: non-english -- https://master.php.net/note/delete/87729/non-english Del: in docs -- https://master.php.net/note/delete/87729/in+docs Del: other reasons-- https://master.php.net/note/delete/87729 Reject -- https://master.php.net/note/reject/87729 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#148310) next »