Splitting by a semicolon not surrounded by quote signs

后端 未结 3 2038
感情败类
感情败类 2021-01-23 11:07

Well, hello community. I\'m workin\' on a CSV decoder in PHP (yeah, I know there\'s already one, but as a challenge for me, since I\'m learning it in my free time). Now the prob

3条回答
  •  半阙折子戏
    2021-01-23 12:04

    It's a bit counter-intuitive, but the simplest way to split a string by regex is often to use preg_match_all in place of preg_split:

    preg_match_all('~("[^"]*"|[^;"]*)(?:;|$)~A', $line, $m);
    $res[] = $m[1];
    

    The A modifier ensures the contiguity of the successive matches from the start of the string.

    If you don't want the quotes to be included in the result, you can use the branch reset feature (?|..(..)..|..(..)..):

    preg_match_all('~(?|"([^"]*)"|([^;"]*))(?:;|$)~A', $line, $m);
    

    Other workaround, but this time for preg_split: include the part you want to avoid before the delimiter and discard it from the whole match using the \K feature:

    $res[] = preg_split('~(?:"[^"]*")?\K;~', $line);
    

提交回复
热议问题