<?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);
}