3v4l.org

run code in 300+ PHP versions simultaneously
<?php $str = "19018216307,Public,\,k]'=system1-system2,20230914143505.5,1-050000,No"; $pattern = <<<'REGEX' ~(?nxx) (?# modifiers: - inline n: parenthesis act as non-capturing groups - inline xx: spaces are ignored even in character classes - global A: all the matches have to be contiguous ) # pattern ( (?!\A) , \K | \A ) # not at the start with a commas or at the start without [^ , \\ ]* ( \\ . [^ , \\ ]* )* # field content (all that isn't a comma nor # a backslash, or an escaped character) # check ( \z (*:END) )? # define a marker if the end of the string is reached ~A REGEX; if (preg_match_all($pattern, $str, $m) && isset($m['MARK'])) { $result = array_map(fn($s) => strtr($s, ['\\\\' => '\\', '\\' => '']), $m[0]); print_r($result); }
Output for 8.1.24 - 8.1.33, 8.2.11 - 8.2.29, 8.3.0 - 8.3.26, 8.4.1 - 8.4.13
Array ( [0] => 19018216307 [1] => Public [2] => ,k]'=system1-system2 [3] => 20230914143505.5 [4] => 1-050000 [5] => No )

preferences:
81.04 ms | 406 KiB | 5 Q