Spliting CSV (comma seperated) entry grouped by double quotes
By using fgetcsv function we can have well formatted output for csv data without any pain. If you don’t want to use fgetcsv function for some reason and your CSV comma separated entries may contain comma inside except the delimiters, here is a quick alternate.
For example we have two rows in our CSV file as below:
DiabetesStore.Com,9833263, www.DiabetesStore.Com,0.0,0.0,0,0,0,1,0,0.0,0.0,0.0,0.0 "Expedia, Inc",10514985,USA - 88x31 - Expedia Logo,0.0,0.0,0,0,0,3,0,0.0,0.0,0.0,0.0
Here is a piece of regular expression which performs it well:
$string="\"Expedia, Inc\", 10514985, USA - 88x31 - Expedia Logo, 0.0, 0.0, 0, 0, 0,3,0,0.0, 0.0, 0.0, 0.0"; $data = preg_split( "/[,]*\\\"([^\\\"]+)\\\"[,]*|[,]+/", $string, 0, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
PREG_SPLIT_DELIM_CAPTURE captures the parenthesized (within “(” and “)” ) content within the string while PREG_SPLIT_NO_EMPTY removes the empty array elements which are supposed to be created by splitting the string by very first occurrence (or the last occurrence sometimes).
I hope this helps someone!
Possibly Related posts:
- Converting a text string into comma separated words
Today, i was looking at some old code of mine to find something when i found this little but useful piece of code which i...
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.






Thank you, your code done the job for me.
I was using this http://www.sitepoint.com/forums/php-34/preg_split-regex-csv-type-line-commas-except-if-quotes-168638.html before I came across your solution.
Much obliged.
Good Stuff!
Alternately, one can use use this smart preg chunk of code found at php.net which takes care of strings starting with quoted values as stated by you.
function csv_string_to_array($str){
$expr=”/,(?=(?:[^\"]*\”[^\"]*\”)*(?![^\"]*\”))/”;
$results=preg_split($expr,trim($str));
return preg_replace(“/^\”(.*)\”$/”,”$1″,$results);
}