|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #78907 SplFileObject::fgetcsv() truncates data after CR char
Submitted: 2019-12-04 07:26 UTC Modified: 2019-12-04 08:16 UTC
Avg. Score:4.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:0 (0.0%)
From: pandenitz at mail dot ru Assigned:
Status: Verified Package: SPL related
PHP Version: Irrelevant OS: Ubuntu 16.0.4
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2019-12-04 07:26 UTC] pandenitz at mail dot ru
Tested on PHP 7.1.26 - 7.4.0 


The initial CSV file has CRLF line breaks.
4th row (starting from 0) has CR inside.

The parser truncates the chars after this CR and the line is incompletely parsed into an array.

Test script:

Expected result:
The row should be parsed correctly with an array element that contains CR inside because CRLF is the real linebreak.

Actual result:
The row is truncated after CR.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2019-12-04 08:16 UTC]
-Status: Open +Status: Verified
 [2019-12-04 08:16 UTC]
Firstly, this CSV is invalid, since any linebreaks inside a field
need to be enclosed in $enclosure characters.  Also, you need to
set the READ_CSV flag.  But even if that is done[1], the line is
still skipped.  It works as expected with fgetcsv()[2], though.

[1] <>
[2] <>
 [2019-12-04 08:45 UTC] pandenitz at mail dot ru
See modified with enclosures added, auto_detect_line_endings = 0 and "\0" escape (ntw why not "\\" ?).

The code has two variants for both SplFileObject::fgetcsv() and usual fgetcsv()

SplFileObject::fgetcsv() is still incorrect.

CSV can or can not have enclosures. It fails even with CR inside the enclosed column.
 [2019-12-04 09:44 UTC] pandenitz at mail dot ru
And SplFileObject::READ_CSV flag is not required for SplFileObject::fgetcsv()

It's required for SplFileObject::current() and SplFileObject::next(), or I am wrong?
 [2019-12-14 15:04 UTC] chrispmaiden at gmail dot com

I'm not sure how relevant this is but I find that if I remove the SplFileObject::DROP_NEW_LINE flag from the code at, I get a pass for both cases...

The docs about this flag at state: "" so perhaps that's what's happening with the SplFileObject case?

Best regards,

 [2019-12-14 15:29 UTC] chrispmaiden at gmail dot com
Sorry, I meant to paste what the docs say about the flag: "Drop newlines at the end of a line." so I guess that's what's happening?
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Apr 22 16:01:31 2024 UTC