3v4l.org

run code in 200+ PHP versions simultaneously
<?php $temp = tempnam(sys_get_temp_dir(), 'csv_bug'); file_put_contents($temp, <<<'CSV' first,second foo,bar "foo baz",bar baz,bar CSV ); $expected = array( array('first', 'second'), array('foo', 'bar'), array("foo\nbaz", 'bar'), array('baz', 'bar'), ); $cases = array( 'drop newline' => \SplFileObject::DROP_NEW_LINE, 'skip empty' => \SplFileObject::SKIP_EMPTY, 'skip empty + read ahead' => \SplFileObject::SKIP_EMPTY | \SplFileObject::READ_AHEAD, 'drop newline + read ahead' => \SplFileObject::DROP_NEW_LINE | \SplFileObject::READ_AHEAD, 'drop newline + skip empty' => \SplFileObject::DROP_NEW_LINE | \SplFileObject::SKIP_EMPTY, 'all' => \SplFileObject::DROP_NEW_LINE | \SplFileObject::SKIP_EMPTY | \SplFileObject::READ_AHEAD, ); foreach ($cases as $name => $flags) { $file = new SplFileObject($temp); $file->setFlags($flags | \SplFileObject::READ_CSV); echo $name, "\n"; var_dump(iterator_to_array($file, false)); } var_dump('expected', $expected);
based on 7uQvB
Output for 7.1.25 - 7.2.14, 7.2.16 - 7.2.18, 7.3.0 - 7.3.4, 7.3.6 - 7.4.0
drop newline array(5) { [0]=> array(2) { [0]=> string(5) "first" [1]=> string(6) "second" } [1]=> array(2) { [0]=> string(3) "foo" [1]=> string(3) "bar" } [2]=> array(1) { [0]=> NULL } [3]=> array(2) { [0]=> string(6) "foobaz" [1]=> string(3) "bar" } [4]=> array(2) { [0]=> string(3) "baz" [1]=> string(3) "bar" } } skip empty array(5) { [0]=> array(2) { [0]=> string(5) "first" [1]=> string(6) "second" } [1]=> array(2) { [0]=> string(3) "foo" [1]=> string(3) "bar" } [2]=> array(1) { [0]=> NULL } [3]=> array(2) { [0]=> string(7) "foo baz" [1]=> string(3) "bar" } [4]=> array(2) { [0]=> string(3) "baz" [1]=> string(3) "bar" } } skip empty + read ahead array(5) { [0]=> array(2) { [0]=> string(5) "first" [1]=> string(6) "second" } [1]=> array(2) { [0]=> string(3) "foo" [1]=> string(3) "bar" } [2]=> array(1) { [0]=> NULL } [3]=> array(2) { [0]=> string(7) "foo baz" [1]=> string(3) "bar" } [4]=> array(2) { [0]=> string(3) "baz" [1]=> string(3) "bar" } } drop newline + read ahead array(5) { [0]=> array(2) { [0]=> string(5) "first" [1]=> string(6) "second" } [1]=> array(2) { [0]=> string(3) "foo" [1]=> string(3) "bar" } [2]=> array(1) { [0]=> NULL } [3]=> array(2) { [0]=> string(6) "foobaz" [1]=> string(3) "bar" } [4]=> array(2) { [0]=> string(3) "baz" [1]=> string(3) "bar" } } drop newline + skip empty array(4) { [0]=> array(2) { [0]=> string(5) "first" [1]=> string(6) "second" } [1]=> array(2) { [0]=> string(3) "foo" [1]=> string(3) "bar" } [2]=> array(2) { [0]=> string(6) "foobaz" [1]=> string(3) "bar" } [3]=> array(2) { [0]=> string(3) "baz" [1]=> string(3) "bar" } } all array(4) { [0]=> array(2) { [0]=> string(5) "first" [1]=> string(6) "second" } [1]=> array(2) { [0]=> string(3) "foo" [1]=> string(3) "bar" } [2]=> array(2) { [0]=> string(6) "foobaz" [1]=> string(3) "bar" } [3]=> array(2) { [0]=> string(3) "baz" [1]=> string(3) "bar" } } string(8) "expected" array(4) { [0]=> array(2) { [0]=> string(5) "first" [1]=> string(6) "second" } [1]=> array(2) { [0]=> string(3) "foo" [1]=> string(3) "bar" } [2]=> array(2) { [0]=> string(7) "foo baz" [1]=> string(3) "bar" } [3]=> array(2) { [0]=> string(3) "baz" [1]=> string(3) "bar" } }
Output for 7.3.5
drop newline array(5) { [0]=> array(2) { [0]=> string(5) "first" [1]=> string(6) "second" } [1]=> array(2) { [0]=> string(3) "foo" [1]=> string(3) "bar" } [2]=> array(1) { [0]=> NULL } [3]=> array(2) { [0]=> string(6) "foobaz" [1]=> string(3) "bar" } [4]=> array(2) { [0]=> string(3) "baz" [1]=> string(3) "bar" } } skip empty array(5) { [0]=> array(2) { [0]=> string(5) "first" [1]=> string(6) "second" } [1]=> array(2) { [0]=> string(3) "foo" [1]=> string(3) "bar" } [2]=> array(1) { [0]=> NULL } [3]=> array(2) { [0]=> string(7) "foo baz" [1]=> string(3) "bar" } [4]=> array(2) { [0]=> string(3) "baz" [1]=> string(3) "bar" } } skip empty + read ahead array(5) { [0]=> array(2) { [0]=> string(5) "first" [1]=> string(6) "second" } [1]=> array(2) { [0]=> string(3) "foo" [1]=> string(3) "bar" } [2]=> array(1) { [0]=> NULL } [3]=> array(2) { [0]=> string(7) "foo baz" [1]=> string(3) "bar" } [4]=> array(2) { [0]=> string(3) "baz" [1]=> string(3) "bar" } } drop newline + read ahead array(5) { [0]=> array(2) { [0]=> string(5) "first" [1]=> string(6) "second" } [1]=> array(2) { [0]=> string(3) "foo" [1]=> string(3) "bar" } [2]=> array(1) { [0]=> NULL } [3]=> array(2) { [0]=> string(6) "foobaz" [1]=> string(3) "bar" } [4]=> array(2) { [0]=> string(3) "baz" [1]=> string(3) "bar" } } drop newline + skip empty array(4) { [0]=> array(2) { [0]=> string(5) "first" [1]=> string(6) "second" } [1]=> array(2) { [0]=> string(3) "foo" [1]=> string(3) "bar" } [2]=> array(2) { [0]=> string(6) "foobaz" [1]=> string(3) "bar" } [3]=> array(2) { [0]=> string(3) "baz" [1]=> string(3) "bar" } } Fatal error: Uncaught RuntimeException: SplFileObject::__construct(/tmp/csv_bugmzrQIs): failed to open stream: No such file or directory in /in/MbRUS:31 Stack trace: #0 /in/MbRUS(31): SplFileObject->__construct('/tmp/csv_bugmzr...') #1 {main} thrown in /in/MbRUS on line 31
Process exited with code 255.
Output for 7.2.19
drop newline array(5) { [0]=> array(2) { [0]=> string(5) "first" [1]=> string(6) "second" } [1]=> array(2) { [0]=> string(3) "foo" [1]=> string(3) "bar" } [2]=> array(1) { [0]=> NULL } [3]=> array(2) { [0]=> string(6) "foobaz" [1]=> string(3) "bar" } [4]=> array(2) { [0]=> string(3) "baz" [1]=> string(3) "bar" } } skip empty array(5) { [0]=> array(2) { [0]=> string(5) "first" [1]=> string(6) "second" } [1]=> array(2) { [0]=> string(3) "foo" [1]=> string(3) "bar" } [2]=> array(1) { [0]=> NULL } [3]=> array(2) { [0]=> string(7) "foo baz" [1]=> string(3) "bar" } [4]=> array(2) { [0]=> string(3) "baz" [1]=> string(3) "bar" } } skip empty + read ahead array(5) { [0]=> array(2) { [0]=> string(5) "first" [1]=> string(6) "second" } [1]=> array(2) { [0]=> string(3) "foo" [1]=> string(3) "bar" } [2]=> array(1) { [0]=> NULL } [3]=> array(2) { [0]=> string(7) "foo baz" [1]=> string(3) "bar" } [4]=> array(2) { [0]=> string(3) "baz" [1]=> string(3) "bar" } } drop newline + read ahead array(5) { [0]=> array(2) { [0]=> string(5) "first" [1]=> string(6) "second" } [1]=> array(2) { [0]=> string(3) "foo" [1]=> string(3) "bar" } [2]=> array(1) { [0]=> NULL } [3]=> array(2) { [0]=> string(6) "foobaz" [1]=> string(3) "bar" } [4]=> array(2) { [0]=> string(3) "baz" [1]=> string(3) "bar" } } Fatal error: Uncaught RuntimeException: SplFileObject::__construct(/tmp/csv_bugXcl0Kv): failed to open stream: No such file or directory in /in/MbRUS:31 Stack trace: #0 /in/MbRUS(31): SplFileObject->__construct('/tmp/csv_bugXcl...') #1 {main} thrown in /in/MbRUS on line 31
Process exited with code 255.
Output for 7.2.15
drop newline array(5) { [0]=> array(2) { [0]=> string(5) "first" [1]=> string(6) "second" } [1]=> array(2) { [0]=> string(3) "foo" [1]=> string(3) "bar" } [2]=> array(1) { [0]=> NULL } [3]=> array(2) { [0]=> string(6) "foobaz" [1]=> string(3) "bar" } [4]=> array(2) { [0]=> string(3) "baz" [1]=> string(3) "bar" } } skip empty array(5) { [0]=> array(2) { [0]=> string(5) "first" [1]=> string(6) "second" } [1]=> array(2) { [0]=> string(3) "foo" [1]=> string(3) "bar" } [2]=> array(1) { [0]=> NULL } [3]=> array(2) { [0]=> string(7) "foo baz" [1]=> string(3) "bar" } [4]=> array(2) { [0]=> string(3) "baz" [1]=> string(3) "bar" } } skip empty + read ahead array(5) { [0]=> array(2) { [0]=> string(5) "first" [1]=> string(6) "second" } [1]=> array(2) { [0]=> string(3) "foo" [1]=> string(3) "bar" } [2]=> array(1) { [0]=> NULL } [3]=> array(2) { [0]=> string(7) "foo baz" [1]=> string(3) "bar" } [4]=> array(2) { [0]=> string(3) "baz" [1]=> string(3) "bar" } } Fatal error: Uncaught RuntimeException: SplFileObject::__construct(/tmp/csv_bugEVndw0): failed to open stream: No such file or directory in /in/MbRUS:31 Stack trace: #0 /in/MbRUS(31): SplFileObject->__construct('/tmp/csv_bugEVn...') #1 {main} thrown in /in/MbRUS on line 31
Process exited with code 255.