简体   繁体   中英

PHP regex robust splitting by comma, ignoring quoted items

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, eg:

$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.

You're parsing a CSV string, so you can use:

$result = str_getcsv( $items);

This will result in:

array(4) {
  [0]=>
  string(3) "one"
  [1]=>
  string(11) "two , three"
  [2]=>
  string(4) "four"
  [3]=>
  string(7) "   five"
}

You can then remove all of the whitespace surrounding the elements with:

$result = array_map( 'trim', $result);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM