Bug #69181 [NEW]: csv parsing drops newlines within fields with SplFileObject::DROP_NEW_LINE
| From: | greg dot bowler at g105b dot com | Date: | Wed, 04 Mar 2015 11:44:54 +0000 |
| Subject: | Bug #69181 [NEW]: csv parsing drops newlines within fields with SplFileObject::DROP_NEW_LINE | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-191130@lists.php.net to get a copy of this message | ||
From: greg dot bowler at g105b dot com
Operating system: Linux Ubuntu 14.04 LTS
PHP version: 5.6.6
Package: Filesystem function related
Bug Type: Bug
Bug description:csv parsing drops newlines within fields with SplFileObject::DROP_NEW_LINE
Description:
------------
When using SplFileObject to parse a CSV file, unexpected behaviour
occurs.
According to the documentation, the DROP_NEW_LINE flag drops newlines at
the end of a line.
http://php.net/manual/en/class.splfileobject.php#splfileobject.constants.drop-new-line
Tests below show that not only the new-lines at the end of the file are
dropped when using the flag, but the first new line in any field is also
dropped.
For example, the following raw csv:
col1,col2
testValue,one\ntwo\nthree
is interpreted like this when using DROP_NEW_LINES flag:
col1,col2
testValue,onetwo\nthree
Test script:
---------------
// Create a new CSV file using good old fputcsv:
$csvLines = [
["Name","Notes"],
["Person 1","This row has no new line"],
["Person 2","This row has\nthree\nnew\nlines"],
];
$filePath = "/tmp/data.csv";
$fp = fopen($filePath, "w");
foreach($csvLines as $line) {
fputcsv($fp, $line);
}
fclose($fp);
// Iteration 1, using DROP_NEW_LINE:
$file = new SplFileObject($filePath, "r");
$file->setFlags(
SplFileObject::READ_CSV |
SplFileObject::READ_AHEAD |
SplFileObject::SKIP_EMPTY |
SplFileObject::DROP_NEW_LINE
);
foreach($file as $i => $row) {
printf("%d \\n in %d ... " . PHP_EOL, substr_count($row[1], "\n"),
$i);;
}
echo PHP_EOL . PHP_EOL;
// Iteration 2, with no DROP_NEW_LINE flag
$file = new SplFileObject($filePath, "r");
$file->setFlags(
SplFileObject::READ_CSV |
SplFileObject::READ_AHEAD |
SplFileObject::SKIP_EMPTY
);
foreach($file as $i => $row) {
printf("%d \\n in %d ... " . PHP_EOL, substr_count($row[1], "\n"),
$i);;
}
Expected result:
----------------
Expected result is that the count of newlines in each iteration is the
same.
There are no newlines at the end of the lines, so there should be none
dropped.
Actual result:
--------------
Output of above test script:
0 \n in 0 ...
0 \n in 1 ...
2 \n in 2 ...
0 \n in 0 ...
0 \n in 1 ...
3 \n in 2 ...
Actual result is that the count of newlines is always 1 less than
expected. The first \n character within any field is stripped.
--
Edit bug report at https://bugs.php.net/bug.php?id=69181&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=69181&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=69181&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=69181&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=69181&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=69181&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=69181&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=69181&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=69181&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=69181&r=support
Expected behavior: https://bugs.php.net/fix.php?id=69181&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=69181&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=69181&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=69181&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=69181&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=69181&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=69181&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=69181&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=69181&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=69181&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=69181&r=mysqlcfg