Bug #13950 Updated: fgetcsv misinterprets fields containing double-quotes

From: Date: Thu, 08 Nov 2001 21:22:46 +0000
Subject: Bug #13950 Updated: fgetcsv misinterprets fields containing double-quotes
References: 1  Groups: php.dev 
Request: Send a blank email to php-dev+get-69834@lists.php.net to get a copy of this message
ID: 13950 User updated by: liamr@umich.edu Reported By: liamr@umich.edu Status: Bogus Bug Type: Filesystem function related Operating System: Sun (SPARC) Solaris 2.6 PHP Version: 4.0.6 / 4.20cvs New Comment: 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. Previous Comments: ------------------------------------------------------------------------ [2001-11-08 15:33:14] jeroen@php.net 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] 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

« previous php.dev (#69834) next »