3v4l.org

run code in 300+ PHP versions simultaneously
<?php $str = <<<'EOD' 19018216307,Public,\,k]'=system1-system2,20230914143505.5,1-050000,No 32432151322,Public,blublabliblo,1324651651654.5,1-050000,No EOD; // change $filePath with the path of your file $filePath = 'data:text/plain;base64,' . base64_encode($str); if (false === $handle = fopen($filePath, 'r')) die("unable to open file $filePath"); $pattern = <<<'REGEX' ~(?nxx) (?# modifiers: - inline n: parenthesis act as a non-capturing group - inline xx: white-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, except escaped characters) # final check ( \z (*:END) )? # define a marker if the end of the string is reached ~A REGEX; $result = []; // stream_get_line doesn't return the newline sequence (3rd parameter) // change it to \r\n or \r if needed, or use fgets and change \z with $ in the pattern while(false !== $line = stream_get_line($handle, 1024, "\n")) { if (preg_match_all($pattern, $line, $m) && isset($m['MARK'])) { $fields = array_map(fn($s) => strtr($s, ['\\\\' => '\\', '\\' => '']), $m[0]); $result[] = $fields; } else { die("unable to parse the following line:\n$line"); } } fclose($handle); print_r($result);

preferences:
36.55 ms | 411 KiB | 5 Q