3v4l.org

run code in 300+ PHP versions simultaneously
<?php $crlf = chr(13) . chr(10); $cr = chr(13); $delimiter = '|'; $enclosure = '"'; //$escape = "\\"; $escape = "\0"; ini_set('auto_detect_line_endings', false); // 7 rows, 5th row has CR linebreak inside $lines = [ 'A', 'a', 'C15|C18|C21|C36|C42|C45|C48|C51|C54|C60|C63|C66|C69|C72|C75|C81|C84|C90|C93|C96|C99|C102|C105|C108|C111', '02-DEC-19|02-DEC-19|707403|GUEST|||PPPPPP||||||RR||||RRSSIAN||FFFF|||||+1111-11-11-111|', '02-DEC-19|02-DEC-19|707404|GUEST|SDDDDD|ВВВВВВВВВВВВ|PPPPPP|СССССС|ПППППП||01.01.1111|M|RR|ППППП|"ПППППППП кккк' . $cr . '"|MISSING LINE CONTENTS|', 'FF_FFFF|FF_1', '|1', ]; $csv = implode($crlf, $lines); $filePath = tempnam(sys_get_temp_dir(), 'csv'); file_put_contents($filePath, $csv); $file = new \SplFileObject($filePath, 'r'); $file->setFlags(SplFileObject::SKIP_EMPTY | SplFileObject::DROP_NEW_LINE | SplFileObject::READ_CSV); $rows = []; while (!$file->eof()) { $row = $file->fgetcsv($delimiter, $enclosure, $escape); if ($row === null) { print_r("NULL result found though `SplFileObject::SKIP_EMPTY | SplFileObject::DROP_NEW_LINE` flags set.\n"); } else { $rows[] = implode($delimiter, $row); } } if (str_replace($enclosure, '', implode($crlf, $lines)) !== implode($crlf, $rows)) { print_r("SplFileObject::fgetcsv() error! Expected:\n"); print_r($lines); print_r("Parsed as:\n"); print_r($rows); } else { print_r("SplFileObject::fgetcsv() passed."); } unset($file); // via fopen print_r("\n\n\n"); $file = fopen($filePath, 'r'); $rows = []; while (!feof($file)) { $row = fgetcsv($file, 0, $delimiter, $enclosure, $escape); if ($row === null) { print_r("NULL result found\n"); } else { $rows[] = implode($delimiter, $row); } } if (str_replace($enclosure, '', implode($crlf, $lines)) !== implode($crlf, $rows)) { print_r("fgetcsv() error! Expected:\n"); print_r($lines); print_r("Parsed as:\n"); print_r($rows); } else { print_r("fgetcsv() passed."); }

preferences:
16.11 ms | 417 KiB | 5 Q