@ 2019-12-04T08:35:31Z <?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.");
}
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 7.4.20 - 7.4.33 , 8.0.7 - 8.0.30 , 8.1.0 - 8.1.28 , 8.2.0 - 8.2.18 , 8.3.0 - 8.3.4 , 8.3.6 - 8.3.7 SplFileObject::fgetcsv() passed.
fgetcsv() passed. Output for 8.3.5 Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
SplFileObject::fgetcsv() passed.
fgetcsv() passed. Output for 7.1.26 - 7.1.33 , 7.2.17 - 7.2.33 , 7.3.0 - 7.3.33 , 7.4.0 - 7.4.13 , 8.0.0 - 8.0.6 SplFileObject::fgetcsv() error! Expected:
Array
(
[0] => A
[1] => a
[2] => 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
[3] => 02-DEC-19|02-DEC-19|707403|GUEST|||PPPPPP||||||RR||||RRSSIAN||FFFF|||||+1111-11-11-111|
[4] => 02-DEC-19|02-DEC-19|707404|GUEST|SDDDDD|ВВВВВВВВВВВВ|PPPPPP|СССССС|ПППППП||01.01.1111|M|RR|ППППП|"ПППППППП кккк
"|MISSING LINE CONTENTS|
[5] => FF_FFFF|FF_1
[6] => |1
)
Parsed as:
Array
(
[0] => A
[1] => a
[2] => 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
[3] => 02-DEC-19|02-DEC-19|707403|GUEST|||PPPPPP||||||RR||||RRSSIAN||FFFF|||||+1111-11-11-111|
[4] => 02-DEC-19|02-DEC-19|707404|GUEST|SDDDDD|ВВВВВВВВВВВВ|PPPPPP|СССССС|ПППППП||01.01.1111|M|RR|ППППП|ПППППППП ккккFF_FFFF|FF_1
|1
)
fgetcsv() passed. preferences:dark mode live preview
180.17 ms | 404 KiB | 165 Q