php.net |  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: 2017-10-24 07:18 UTC
Votes:13
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:
Status: Open Package: Filesystem function related
PHP Version: 5.3.2 OS: All
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: kulakov74 at yandex dot ru
New email:
PHP Version: OS:

 

 [2010-04-07 14:34 UTC] 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 ...

Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-04-08 06:58 UTC] aharvey@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: aharvey
 [2010-04-08 06:58 UTC] aharvey@php.net
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,',','"');
print_r($a);

This will output
Array
(
    [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:
NULL
'\0'

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] datibbaw@php.net
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] cmb@php.net
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] kalle@php.net
-Status: Assigned +Status: Open -Assigned To: aharvey +Assigned To:
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC