|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44074 fgetcsv incorrect parse
Submitted: 2008-02-07 20:37 UTC Modified: 2008-02-10 14:35 UTC
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: felixl at densi dot com Assigned:
Status: Wont fix Package: Streams related
PHP Version: 5.2.5 OS: Windows XP SP2 & linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
Solve the problem:
50 - 9 = ?
Subscribe to this entry?

 [2008-02-07 20:37 UTC] felixl at densi dot com
a csv file with 5 columns.

note the ^M in data, it's suppose to be there
note the \" line

incorrect parse of the line with the \"
remove the \ and the parse is correct.

As per csv doc, when inside a quote string ("), enter, ^M or anything else then " is consider data.

PS. I wanted to attach my files but it seems there is no way to do that.

So you might not see the ^M character in the test file.

Reproduce code:
test file (test.csv):

050855,0,20071114,19:01:08,"Don returned my call.

We had a nice talk about things. 

He is slowing down a bit. 

He said he got his order and the drums were a little better wrapped.

050855,0,20071220,11:33:44,"nice call with Don.

He's pretty lonely right now."
050855,0,20071220,12:39:16,finally e-mailed him...
050855,0,20080108,10:31:00,"I calle Don and we chatted for a bit."

script (readcsv.php):


    $numFields = $argv[1];
    $file = $argv[2];

    print "numFields: $numFields\n";
    print "Filename: $file\n";

    $num = 0;
    $limit = 200;

    $handle = fopen($file, 'r');

    while (($arr = fgetcsv($handle)) !== false) {
    	if ($num % $limit == 0) {
    		print "$num\n";
        if (count($arr) != $numFields) {
            print "Invalid number of column at $num\n";



    print "Done\n";

usage: php readcsv.php 5 test.csv

Expected result:
numFields: 5
Filename: fgetcsv\test.csv

Actual result:
numFields: 5
Filename: fgetcsv\test.csv
Invalid number of column at 2
    [0] => He's pretty lonely right now."


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2008-02-08 00:00 UTC]
Hello, note that the escape character default is the backslash. Change it to other character, and you'll the expected result.
 [2008-02-08 13:28 UTC] felixl at densi dot com
as per fgetcsv doc:

php version 5.3.0  	 The escape  parameter was added

so it's not possible with version 5.2.5


from wikipedia csv:

However CSV differs from other delimiter separated file formats in using a " (double quote) character around fields that contain reserved characters

so the default should be " and not \
 [2008-02-10 14:35 UTC]
The current behavior is maintained for BC reasons.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 17 07:01:27 2024 UTC