fgetcsv不读取附件 [英] fgetcsv not reading enclosures
问题描述
我正在尝试使用fgetcsv
读取CSV行.但是,似乎根本不在乎机箱.
I am trying to read a CSV line using fgetcsv
. However it seem not to care at all about enclosures.
这是该行的样子:
Super Administrator,"ROLE_SUPER_ADMIN, ROLE_GROUP_GUEST, ROLE_GROUP_WRITER, ROLE_USER_WRITER, ROLE_USER_GUEST"
这是CSV标准的有效行,它应返回以下内容,并以"
作为附件,以,
作为分隔符:
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',
);
但是这是我得到的:
$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"',
);
是的,看来fgetcsv
完全忽略了机箱字符.我尝试使用fgets
然后使用str_getcsv
使其正常工作,但是结果是相同的-str_getcsv
很可能是fgetcsv
调用的,无论如何还是要进行CSV转换.
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.
我认为这可能是某种回归错误,该错误已在我使用的PHP版本中找到了,所以我在此处粘贴php -v
命令的结果:
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
我相信这是我撰写本文时的最新版本的PHP5.尽管我怀疑这是某种PHP错误,但也可能是一些安装/配置问题,因此为什么我要发布此问题.
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?
正如Mark Baker所指出的那样,PHP5.5.27-1实际上是当时的PHP5.5的最新版本.
As pointed out by Mark Baker, PHP5.5.27-1 is actually the latest revision of PHP5.5 at the time being.
尝试使用bin2hex
功能,并显示var_dump
.
EDIT 2:
Here is an attempt with the bin2hex
function, displayed with a var_dump
.
行读取",",","
(每个单元格中两个单元格仅包含一个逗号):0022002c0022002c0022002c0022000a
.
Line read ",",","
(2 cells containing only a coma in each cell): 0022002c0022002c0022002c0022000a
.
对于bin2hex('"')
,结果仅为22
.
str_getcsv
仍然给我这个错误.
推荐答案
好的,解决了.
这就是每个人都在怀疑的:文件的编码混乱了.我不知道这是哪种编码,但是每当我尝试打开CSV时,LibreOffice都向我提出Unicode.
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.
我不得不用nano打开它们,以意识到确实存在编码问题.我在计算机上使用的Gedit,vim或任何其他工具均未出现任何错误.用nano打开时,在每隔两个字符之间插入一个@
符号,并且换行不正确.
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.
fgetcsv
似乎不支持某些编码.为了解决该问题,我从nano(从另一个未显示@
的工具复制粘贴)中重新创建了文件.
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 @
).
这篇关于fgetcsv不读取附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!