|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2001-11-06 00:23 UTC] liamr at umich dot 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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 05 19:00:01 2025 UTC |
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.