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!