I would like to robustly split a list by commas. As-is, preg_split
is accomplishing the basic goal. The user can enter any combination of [space(s)],[space(s)]
between items and the list will split out successfully.
$items = 'one, two , three ,four, five';
$items = preg_split('/(\s*,\s*)+/', $items);
This results in ['one', 'two', 'three', 'four', 'five']
correctly. I would like to augment this to allow for escaped delimiters via quotations, e.g.:
$items = 'one, "two , three",four, five';
Desired result of ['one', 'two , three', 'four', 'five']
I believe the answer is in preg_match_all
, but can't seem to put the puzzle pieces together with the unique [space(s)],[space(s)]
constraint.
Note that str_getcsv
doesn't work in this case, as the spacing will skew the final strings.