note 62524 modified in function.fgetcsv by danbrown

From: Date: Sun, 17 May 2009 18:15:05 +0000
Subject: note 62524 modified in function.fgetcsv by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-155085@lists.php.net to get a copy of this message
For those of you who need to get CSV data from a single line of quoted CSV (comma delimited) values (that may have commas in the data), use this: <?php function csv_string_to_array($str){ $expr="/,(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/"; $results=preg_split($expr,trim($str)); return preg_replace("/^\"(.*)\"$/","$1",$results); } $str=<<<EOF "testing, stuff, here","is testing ok",200,456 EOF; print_r(csv_string_to_array($str)); ?> --was-- For those of you who need to get CSV data from a single line of quoted CSV (comma delimited) values (that may have commas in the data), use this: <? function csv_string_to_array($str){ $expr="/,(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/"; $results=preg_split($expr,trim($str)); return preg_replace("/^\"(.*)\"$/","$1",$results); } $str=<<<EOF "testing, stuff, here","is testing ok",200,456 EOF; print_r(csv_string_to_array($str)); ?> http://php.net/manual/en/function.fgetcsv.php

« previous php.notes (#155085) next »