|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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 ...
PatchesPull Requests
Pull requests:
HistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Nov 21 13:00:01 2025 UTC |
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 )