PHP split comma-separated values but retain quotes

Tags: , , , ,

I’m trying to split a string that contains comma-separated set of values. This can be achieved simply by using str_getcsv but I have an additional requirement where it falls short of. I need to retain quotes.

With an input string of:

string(30) "Hello, "San Diego, California""

I tried two approaches:


$result = explode(",", $string);

Which results in

array(3) {
  string(5) "Hello"
  string(11) " "San Diego"
  string(12) " California""


$result = str_getcsv($string, ",");

This one results in

array(2) {
  string(5) "Hello"
  string(21) "San Diego, California"

I prefer using str_getcsv because it splits the values properly but it trims the enclosing quotes out. I need those quotes so I’m hoping I could call the function without it automatically removing the quotes.

Additional Info

I am actually open for a regex solution but I am clueless in that area.

I tried the solution here and it didn’t work.


This pushed the limits of my regex knowledge, and I was unable to come up with an elegant regex which covers all possible cases for the input string (e.g. If the string ends with a comma) without leaving empty matches at the end.

$parts = preg_split('/(?:"[^"]*"|)Ks*(,s*|$)/', $string);

By itself, this gives:

    [0] => Hello
    [1] => "San Diego, California"
    [2] => 

And you can clean up the empty elements like this:

$result = array_filter($parts, function ($value) {
    return ($value !== '');

Note: The regex trims white-space from the start/end of each match. Remove the s* parts if you don’t want that.

Source: stackoverflow