Bug #13950 Updated: fgetcsv misinterprets fields containing double-quotes
ID: 13950
Updated by: jeroen
Reported By: liamr@umich.edu
Old Status: Open
Status: Bogus
Bug Type: Filesystem function related
Operating System: Sun (SPARC) Solaris 2.6
PHP Version: 4.0.6 / 4.20cvs
New Comment:
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
Previous Comments:
------------------------------------------------------------------------
[2001-11-08 14:34:20] liamr@umich.edu
I tried it with the copy of PHP in CVS. It's a problem in that version too.
------------------------------------------------------------------------
[2001-11-06 00:23:31] liamr@umich.edu
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 liamr@umich.edu
liam "Liam Hoekenga" liam.hoekenga@umich.edu
lhoek "Liam H." "Liam R. Hoekenga" <liamr@umich.edu>
lrh ME "ME" ME liamr@umich.edu
lrh1 "ME" ME liamr@umich.edu
produces this output:
3 fields in line 1:
liamr
Hoekenga, Liam
liamr@umich.edu
3 fields in line 2:
liam
Liam Hoekenga
liam.hoekenga@umich.edu
3 fields in line 3:
lhoek
Liam H.
Liam R. Hoekenga
3 fields in line 4:
lrh
ME "ME" ME
liamr@umich.edu
3 fields in line 5:
lrh1
ME
liamr@umich.edu
------------------------------------------------------------------------
Edit this bug report at http://bugs.php.net/?id=13950&edit=1
Thread (8 messages)