问题
I am trying to read a CSV line using fgetcsv
. However it seem not to care at all about enclosures.
Here is what the line looks like:
Super Administrator,"ROLE_SUPER_ADMIN, ROLE_GROUP_GUEST, ROLE_GROUP_WRITER, ROLE_USER_WRITER, ROLE_USER_GUEST"
This is a valid line by CSV standards, and it should return the following with "
as the enclosure and ,
as the delimiter:
$l = fgetcsv($handle, 0, ',', '"');
array(
[0] => 'Super Administrator',
[1] => 'ROLE_SUPER_ADMIN, ROLE_GROUP_GUEST, ROLE_GROUP_WRITER, ROLE_USER_WRITER, ROLE_USER_GUEST',
);
However this is what I get:
$l = fgetcsv($handle, 0, ',', '"');
array(
[0] => 'Super Administrator',
[1] => '"ROLE_SUPER_ADMIN',
[2] => ' ROLE_GROUP_GUEST',
[3] => ' ROLE_GROUP_WRITER',
[4] => ' ROLE_USER_WRITER',
[5] => ' ROLE_USER_GUEST"',
);
So yeah, it seems fgetcsv
is right-out ignoring completely the enclosure character. I tried to get it working using fgets
and then str_getcsv
but the result is the same - str_getcsv
is most probably called by fgetcsv
for the CSV conversion anyway.
I figured it could be some sort of regression bug that found its way in the PHP version I'm using, so I'm pasting the result of a php -v
command here:
me@linux:~/$ php -v
PHP 5.5.27-1+deb.sury.org~trusty+1 (cli) (built: Jul 15 2015 12:14:44)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies
I believe this is the latest version of PHP5 at the moment I'm writing this. Although I suspect this is some kind of PHP bug, it could also be some installation/configuration problem, hence why I'm publishing this question.
Has anyone already experienced this? What is the best way to solve this problem?
EDIT:
As pointed out by Mark Baker, PHP5.5.27-1 is actually the latest revision of PHP5.5 at the time being.
EDIT 2:
Here is an attempt with the bin2hex
function, displayed with a var_dump
.
Line read ",",","
(2 cells containing only a coma in each cell): 0022002c0022002c0022002c0022000a
.
With bin2hex('"')
the result is just 22
.
str_getcsv
still gives me this error though.
回答1:
OK, solved.
This is what everyone was suspecting: the encoding of the file was messed up. I could not know which encoding this was, but LibreOffice proposed me Unicode whenever I tried to open the CSVs.
I had to open them with nano to realize there was indeed an encoding problem. Gedit, vim or any other tool I had on my computer raised no errors. When opened with nano, an @
symbol was inserted between every other characters and line feeds were not read correctly.
It seems there are some encodings that are not well supported by fgetcsv
. To solve the problem, I recreated the files from nano (copy-paste from another tool that did not display the @
).
来源:https://stackoverflow.com/questions/31860061/fgetcsv-not-reading-enclosures