php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53848 fgetcsv ignores spaces at beginnings of fields
Submitted: 2011-01-26 13:49 UTC Modified: 2011-05-29 12:21 UTC
Votes:4
Avg. Score:5.0 ± 0.0
Reproduced:4 of 4 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: andre at webkr dot de Assigned: iliaa (profile)
Status: Closed Package: Filesystem function related
PHP Version: 5.3.5 OS: Windows 7
Private report: No CVE-ID: None
 [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') )

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-02-11 14:50 UTC] phillip at grueter-online dot de
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"
}
 [2011-04-15 19:40 UTC] mark dot mccray at tbwachiat dot com
I'm posting a comment here instead of a bug because I feel we may be 
encountering the same issue.

My problem is that fgetcsv seems to trim whitespace at the beginning of any 
field -- not just at the beginning of a record.

In a record (our files are separated by a pipe symbol):
"001|00005964_154|OGLCV|003| 
174699|USD|0326049|Corporation||000000|11/03/27||11/04/14|1|MPY||CP"

The field " 174699" gets inserted into the array as "174699".

We are expecting the leading whitespace to not be trimmed.
 [2011-04-15 19:40 UTC] mark dot mccray at tbwachiat dot com
Forgot to add that we are on hp-ux 11.11i. Php ver 5.2.4.
 [2011-04-15 19:49 UTC] mark dot mccray at tbwachiat dot com
Same issue on Mac OS X 10.6.7 running PHP ver 5.3.4.
 [2011-04-16 19:51 UTC] andre at webkr dot de
-Summary: fgetcsv ignores spaces on beginning of line +Summary: fgetcsv ignores spaces at beginnings of fields
 [2011-04-16 19:51 UTC] andre at webkr dot de
Indeed, I overlooked that.

I added your data to my test (without the quotes and line break, of course) and I can confirm that issue. I changed the summary accordingly.
 [2011-04-21 00:51 UTC] dave at roweware dot com
Same problem for me on Ubuntu 10.04 - PHP 5.3.2

I would much prefer to control if/when the fields are trimmed via trimming them 
myself, or via a flag.
 [2011-05-29 12:21 UTC] iliaa@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: iliaa
 [2011-05-29 12:21 UTC] iliaa@php.net
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 [2011-05-29 12:23 UTC] iliaa@php.net
Automatic comment from SVN on behalf of iliaa
Revision: http://svn.php.net/viewvc/?view=revision&revision=311543
Log: Fixed bug #53848 (fgetcsv() ignores spaces at beginnings of fields).
 [2011-10-10 09:22 UTC] roborg at hotmail dot com
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,
Greg
 [2011-12-01 09:13 UTC] ygautheron at absystech dot fr
I 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
 [2012-04-18 09:50 UTC] laruence@php.net
Automatic comment on behalf of iliaa
Revision: http://git.php.net/?p=php-src.git;a=commit;h=57674f784fa3f0de227a6a8ee717519574b54e17
Log: Fixed bug #53848 (fgetcsv() ignores spaces at beginnings of fields).
 [2012-07-24 23:41 UTC] rasmus@php.net
Automatic comment on behalf of iliaa
Revision: http://git.php.net/?p=php-src.git;a=commit;h=57674f784fa3f0de227a6a8ee717519574b54e17
Log: Fixed bug #53848 (fgetcsv() ignores spaces at beginnings of fields).
 [2013-11-17 09:38 UTC] laruence@php.net
Automatic comment on behalf of iliaa
Revision: http://git.php.net/?p=php-src.git;a=commit;h=57674f784fa3f0de227a6a8ee717519574b54e17
Log: Fixed bug #53848 (fgetcsv() ignores spaces at beginnings of fields).
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 30 14:01:28 2024 UTC