Edit report at http://bugs.php.net/bug.php?id=53848&edit=1
ID: 53848
Comment by: mark dot mccray at tbwachiat dot com
Reported by: andre at webkr dot de
Summary: fgetcsv ignores spaces on beginning of line
Status: Open
Type: Bug
Package: Filesystem function related
Operating System: Windows 7
PHP Version: 5.3.5
Block user comment: N
Private report: N
New Comment:
Same issue on Mac OS X 10.6.7 running PHP ver 5.3.4.
Previous Comments:
------------------------------------------------------------------------
[2011-04-15 19:40:41] 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:40:08] 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-02-11 14:50:52] 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-01-26 13:49:48] 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') )
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=53848&edit=1