|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #51496 fgetcsv should take empty string as an escape
Submitted: 2010-04-07 14:34 UTC Modified: 2018-12-15 13:42 UTC
Avg. Score:3.9 ± 1.0
Reproduced:11 of 11 (100.0%)
Same Version:6 (54.5%)
Same OS:6 (54.5%)
From: kulakov74 at yandex dot ru Assigned: cmb (profile)
Status: Closed Package: Filesystem function related
PHP Version: 5.3.2 OS: All
Private report: No CVE-ID: None
 [2010-04-07 14:34 UTC] kulakov74 at yandex dot ru
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:


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", '"', ""));

Expected result:
array( ... ) - depends on the input file

Actual result:
Warning: fgetcsv(): escape must be character in ...


Add a Patch

Pull Requests

Pull requests:

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2010-04-08 06:58 UTC]
-Status: Open +Status: Assigned -Assigned To: +Assigned To: aharvey
 [2010-04-08 06:58 UTC]
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.
 [2011-07-19 17:43 UTC] conrad1 at gmail dot com
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,',','"');

This will output
    [0] => cell1
    [1] => cell2\",cell3"
    [2] => cell4
 [2012-04-10 21:02 UTC] laurent at chardin dot org
fgetcsv should also take empty strings as an enclosure.
I got the case of dealing with CSV files without enclosures, only comma-delimited 
values. fgetcsv complains when trying to set either of those values:

using '\0' did the trick, but at the cost of a tons of warnings:
fgetcsv(): enclosure must be a single character

Is there another caracter that could act as an empty one ?
 [2014-12-02 14:06 UTC]
I'm currently making my way through all reported issues against CSV related functions in order to fix them for PHP 7.

I believe that this particular issue can be resolved by using the double quote as both the enclosure AND escape character; in fact, this should be the default, because a backslash makes no sense at all if you consider other applications such as Numbers, Excel and Calc that do not treat a backslash as anything special.
 [2016-08-26 14:02 UTC]
Any news here? In my opinion, this feature is a must-have, because
escaping in CSV files with an escape character is pretty uncommon,
and I'm not even sure how $escape is supposed to work currently
(see bug #67566).
 [2017-10-24 07:18 UTC]
-Status: Assigned +Status: Open -Assigned To: aharvey +Assigned To:
 [2018-06-17 23:35 UTC] php at sameprecision dot org
The escape character was probably intended to be an alternative to doubling up the enclosure character: place the escape character in front of the enclosure character and itself (fputcsv doesn't put the escape character in front of itself so it is useless as an escape character).  Friends and I commented on this issue a LONG time ago and have been using a user-land function because fputcsv is a data-lossy function.
 [2018-09-13 12:36 UTC]
<> would implement this
feature request.
 [2018-12-15 13:42 UTC]
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
 [2018-12-15 13:42 UTC]
Implemented as commit 3b0f051[1]; available as of PHP 7.4.0.

[1] <;a=commit;h=3b0f05119383fe21ee75adaed3d0239ba8976aef>.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Jul 22 05:01:29 2024 UTC