ID: 13950
Updated by: jeroen
Reported By: [EMAIL PROTECTED]
Old Status: Bogus
Status: Open
Old Bug Type: Filesystem function related
Bug Type: Documentation problem
Operating System: Sun (SPARC) Solaris 2.6
PHP Version: 4.0.6 / 4.20cvs
New Comment:

The CSV format prescribes that fields may be enclosed in double quotes, to make it 
possible to have the delimiter itself part of a field.

try google on CSV, to find a definition. If you find one, we'd appreciate it if you 
entered a link here, so we can make the documentation better.

Changing to documentation bug (indeed, the docs aren't clear about this)

Previous Comments:
------------------------------------------------------------------------

[2001-11-08 16:22:46] [EMAIL PROTECTED]

How is this /not/ a bug?

It does the same thing indepentdent of delimiter.

If I have a CSV file that looks like
one,"two" three,four
one,two" three",four
one,two,three,four
one,two "three" four,five

and it gets read in as
('one', 'two', 'four')
('one',' two "three"', 'four')
('one', 'two', 'three', 'four')
('one', 'two "three" four', 'five')

That behavoir does not match the function definition of fgetcsv in the manual.  it is 
mishandling the data. 

"fgetcsv --  Gets line from file pointer and parse for CSV fields 

Description

    array fgetcsv (int fp, int length, string [delimiter])
 
Similar to fgets() except that fgetcsv() parses the line it reads for fields in CSV 
format and returns an array containing the fields read. The field delimiter is a 
comma, unless you specify another delimiter with the optional third parameter. "

How can that possibly mean "strips off double quotes and removes subsequent data"?  
I'd expect it to split on the delimiter specified, and to file the resulting 
information into the array.


------------------------------------------------------------------------

[2001-11-08 15:33:14] [EMAIL PROTECTED]

This is exactly what fgetcsv is for (interpreting the double quotes in CSV files). Use 
explode if you simply want to split on tabs.

No bug.

--Jeroen

------------------------------------------------------------------------

[2001-11-08 14:34:20] [EMAIL PROTECTED]

I tried it with the copy of PHP in CVS.  It's a problem in that version too.

------------------------------------------------------------------------

[2001-11-06 00:23:31] [EMAIL PROTECTED]

I'm trying to write a script to interpret pine-style addressbooks, which are pretty 
much just full tab-seperated values..  It doesn't handle fields with double quotes 
correctly.  If the field starts with a quotes, it strips them, and removes any data 
that follows the closing quote for that field.  This code and data will replicated the 
problem.

<?php

$row = 1;
$fh = fopen("./addressbook","r");
while ($data = fgetcsv ($fh, 1000, "\t")) {
    $num = count($data);
    $match = preg_grep("/(#|\(|\))/", $data);
    if ( ($num < 3) || (count($match) >=1) ) {
        continue;
    }

    print "\n$num fields in line $row:\n";
    $row++;
    for ($c=0; $c<$num; $c++) {
        print $data[$c] . "\n";
    }
}
fclose ($fh);

?>

"addressbook" is a tab separated file containing data along the lines of:

liamr   Hoekenga, Liam  [EMAIL PROTECTED]
liam    "Liam Hoekenga" [EMAIL PROTECTED]
lhoek   "Liam H."       "Liam R. Hoekenga" <[EMAIL PROTECTED]>
lrh     ME "ME" ME      [EMAIL PROTECTED]
lrh1    "ME" ME [EMAIL PROTECTED]

produces this output:
3 fields in line 1:
liamr
Hoekenga, Liam
[EMAIL PROTECTED]

3 fields in line 2:
liam
Liam Hoekenga
[EMAIL PROTECTED]

3 fields in line 3:
lhoek
Liam H.
Liam R. Hoekenga

3 fields in line 4:
lrh
ME "ME" ME
[EMAIL PROTECTED]

3 fields in line 5:
lrh1
ME
[EMAIL PROTECTED]

------------------------------------------------------------------------



Edit this bug report at http://bugs.php.net/?id=13950&edit=1

Reply via email to