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:
explode
$result = explode(",", $string);
Which results in
array(3) { [0]=> string(5) "Hello" [1]=> string(11) " "San Diego" [2]=> string(12) " California"" }
str_getcsv
$result = str_getcsv($string, ",");
This one results in
array(2) { [0]=> string(5) "Hello" [1]=> 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.
Advertisement
Answer
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:
Array ( [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.