|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2011-01-26 13:49 UTC] andre at webkr dot de
Description:
------------
RFC4180 says: "Spaces are considered part of a field and should not be ignored."
However (despite being the only CSV parsing function that fulfils all other requirements), fgetcsv ignores spaces at the very beginning of a record.
Test script:
---------------
/*
Put this in a file:
a,b
c,d
*/
$fd = fopen('the_file','r');
$a = fgetcsv($fd);
Expected result:
----------------
array( array('a', 'b'), array(' c','d') )
Actual result:
--------------
array( array('a', 'b'), array('c','d') )
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 21:00:01 2025 UTC |
Same problem in version 5.2.10 shell > php --version PHP 5.2.10-2ubuntu6.5 php > var_dump(file("csvtest.csv")); array(3) { [0]=> string(4) "a,b " [1]=> string(5) " c,d " [2]=> string(1) " " } php > $handle = fopen("csvtest.csv", "r"); php > $a = fgetcsv($handle); php > var_dump($a); array(2) { [0]=> string(1) "a" [1]=> string(1) "b" } php > $a = fgetcsv($handle); php > var_dump($a); array(2) { [0]=> string(1) "c" [1]=> string(1) "d" }Please reopen this bug - it's not working when the delimiter is a tab and an enclosure is used. Test script: ------------ print_r(str_getcsv("1\t2\t3", "\t", "'")); print_r(str_getcsv("1\t\t3", "\t", "")); print_r(str_getcsv("'1'\t\t'3'", "\t", "'")); Expected result: ---------------- Array ( [0] => 1 [1] => 2 [2] => 3 ) Array ( [0] => 1 [1] => [2] => 3 ) Array ( [0] => 1 [1] => [2] => 3 ) Actual result (PHP 5.3.8) ------------------------- Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 3 ) Array ( [0] => 1 [1] => [2] => 3 ) Array ( [0] => 1 [1] => 3 ) Before this patch, the actual result was the same as the expected result. Thanks, GregI see unexpected result too with gentoo : boricare ~ # php -a Interactive shell php > print_r(str_getcsv("1\t2\t3", "\t", "'")); Array ( [0] => 1 [1] => 2 [2] => 3 ) php > print_r(str_getcsv("1\t\t3", "\t", "")); Array ( [0] => 1 [1] => [2] => 3 ) php > print_r(str_getcsv("'1'\t\t'3'", "\t", "'")); Array ( [0] => 1 [1] => 3 ) boricare ~ # php -v PHP 5.3.8-pl0-gentoo (cli) (built: Nov 30 2011 10:13:54) Copyright (c) 1997-2011 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies with Xdebug v2.1.0, Copyright (c) 2002-2010, by Derick Rethans