php.net |  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
Votes:1
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
Have you experienced this issue?
Rate the importance of this bug to you:

 [2008-02-07 20:37 UTC] felixl at densi dot com
Description:
------------
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):

<?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) {
    	$num++;
    	
    	if ($num % $limit == 0) {
    		print "$num\n";
    	}
    	
        if (count($arr) != $numFields) {
            print "Invalid number of column at $num\n";
            print_r($arr);
            exit;
        }

    }

    fclose($handle);

    print "Done\n";
    exit;
?>


usage: php readcsv.php 5 test.csv

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


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


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-02-08 00:00 UTC] felipe@php.net
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

also

from wikipedia csv: http://en.wikipedia.org/wiki/Comma-separated_values

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] iliaa@php.net
The current behavior is maintained for BC reasons.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 10:01:30 2024 UTC