Edit report at https://bugs.php.net/bug.php?id=51496&edit=1
ID: 51496
Comment by: conrad1 at gmail dot com
Reported by: kulakov74 at yandex dot ru
Summary: fgetcsv should take empty string as an escape
Status: Assigned
Type: Feature/Change Request
Package: Filesystem function related
Operating System: All
PHP Version: 5.3.2
Assigned To: aharvey
Block user comment: N
Private report: N
New Comment:
fgetcsv does NOT get the cells right if the last characte from a cell is \
How to replicate:
//you can also try this with fopen('file.csv')
$body = "\"cell1\",\"cell2\\\",\"cell3\",\"cell4\"";
$filename = 'data://text/plain;base64,'.base64_encode($body);
$fp = fopen($filename,"r");
$a = fgetcsv($fp,10000,',','"');
print_r($a);
This will output
Array
(
[0] => cell1
[1] => cell2\",cell3"
[2] => cell4
)
Previous Comments:
------------------------------------------------------------------------
[2010-04-08 06:58:55] [email protected]
Sounds reasonable to me. It probably wouldn't hurt to allow the enclosure to
accept an empty string as well.
I'll cook something up for trunk and we can decide whether we want this in 5.3
from there.
------------------------------------------------------------------------
[2010-04-07 14:34:12] kulakov74 at yandex dot ru
Description:
------------
Currently fgetcsv() gives a warning if the escape parameter is set as an empty
string (and the default is a backslash). I have some data that has backslashes
in it and it's not an escape character. Even though most of the times fgetcsv()
reads the data correctly, there is a little chance it will do it wrong if a
backslash is the last character of a multiline cell, which is usually stored
like this:
"\\\line1
line2\\\"
In order to fix that I supply chr(8) as an escape character because I know for
sure the data does not have the character. And if I pass an empty string
instead fgetcsv() will give a warning and refuse to read a line.
I suggest that fgetcsv() does accept an empty string as an escape and do no
escaping in that case, which is quite usual.
For ex., a MySql statement "Load Data Infile ... Into Table" has it as "Escaped
By None" to achieve the same result.
Test script:
---------------
if (!$Handle=fopen("sites.txt", "rb")) return false;
print_r(fgetcsv($Handle, 0, "\t", '"', ""));
fclose($Handle);
Expected result:
----------------
array( ... ) - depends on the input file
Actual result:
--------------
Warning: fgetcsv(): escape must be character in ...
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=51496&edit=1