Bug #12643 Updated: fgetcsv does some wrong things

From: Date: Tue, 18 Jun 2002 21:49:03 +0000
Subject: Bug #12643 Updated: fgetcsv does some wrong things
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-11205@lists.php.net to get a copy of this message
ID: 12643 Updated by: Grant.Walters@walters.co.nz Reported By: Grant.Walters@walters.co.nz Status: Bogus Bug Type: Filesystem function related Operating System: Linux PHP Version: 4.0.6 New Comment: So as my data that does not contain an "enclosure character", how should this be handled? The current manual page states that the parameter cannot be null. Previous Comments: ------------------------------------------------------------------------ [2002-06-18 08:14:35] hholzgra@php.net you mixed up delimiter and enclosure arguments here and redefined only the delimiter from ',' to '~' you also have to redefine the enclosure character to something you don't use in your data instead of '"' to make your code work (4th parameter to fgetcsv()) ------------------------------------------------------------------------ [2002-06-18 06:34:01] derick@php.net can you see if this is fixed in the latest non-stable cvs snapshot from snaps.php.net ? Derick ------------------------------------------------------------------------ [2001-08-08 10:51:27] sniper@php.net Reproduced with latest CVS of PHP. --Jani ------------------------------------------------------------------------ [2001-08-08 04:56:12] Grant.Walters@walters.co.nz <? /* - fgetcsv when used with non " delimiter causes problems - script called from command line - on a large file (25,000 lines) the script will core dump most times with a segmentation fault Contents of gr DOS (CR/LF) or Unix (LF) record termination makes no difference to result ------------------------------------------ 26261~~5402211~yes~MASTERFULL~0 26263~~0126003045~yes~"PIONEERING"~0 26263~~039300358~yes~ " CASSIOPEIA~0 26263~~91054745~yes~OLYMPIC~0 26261~~2302~yes~MASTERLESS~0 26263~~6003045~yes~PIONEERING~0 ------------------------------------------ */ $file_name = "/tmp/unix/gr"; // Faulty: echo "\nFAULTY\n"; $text_file = fopen($file_name,"r"); if ($text_file) { $count=1; while ($data=fgetcsv($text_file,200,"~")) { echo "Record:$count:".$data[0].":".$data[2].":".$data[4]."\n"; $count++; } echo "$file_name Complete\n"; } fclose($text_file); /* Problem 1: This will stop in the third record because of the unmatched " Problem 2: It strips the " characters out of the resultant array fields */ //Working: echo "\nWORKING\n"; $text_file = fopen($file_name,"r"); if ($text_file) { $count=1; while ($data_in=fgets($text_file,200)) { $data=explode("~",$data_in); echo "Record:$count:".$data[0].":".$data[2].":".$data[4]."\n"; $count++; } echo "$file_name Complete\n"; } fclose($text_file); /* SCRIPT OUTPUT FAULTY Record:1:26261:5402211:MASTERFULL Record:2:26263:0126003045:PIONEERING /tmp/unix/gr Complete WORKING Record:1:26261:5402211:MASTERFULL Record:2:26263:0126003045:"PIONEERING" Record:3:26263:039300358: " CASSIOPEIA Record:4:26263:91054745:OLYMPIC Record:5:26261:2302:MASTERLESS Record:6:26263:6003045:PIONEERING /tmp/unix/gr Complete */ ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=12643&edit=1

« previous php.bugs (#11205) next »