使用 fgetcsv() 解析 csv(用引号引起来的数据)无法正常工作
我正在尝试解析一个 csv 文件,其中我的数据以如下格式给出:
"Total","Shazam (DE)","Total",,"215,611","538","£935.97","£60.77"
现在,如果我给出如下所示的 fgetcsv() :
$values = fgetcsv($f,8098);
它返回一个数组为:
array(9) {
[0]=>
string(15) ""Total""
[1]=>
string(27) ""Shazam (DE)""
[2]=>
string(15) ""Total""
[3]=>
string(1) ""
[4]=>
string(9) ""215"
[5]=>
string(9) "611""
[6]=>
string(11) ""538""
[7]=>
string(19) ""£935.97""
[8]=>
string(19) ""£60.77"
"
}
但现在,如果我给出如下 if 语句:
$values = fgetcsv($f,8098);
if ($values[0] == "Total")
echo "Hello";
它不是回显什么,为什么条件不满足?这与所附的引号有关吗?
我什至尝试转义引号,例如。
if ($values[0] == '\"Total\"')
echo "Hello";
但没有用。
我需要为此更改任何 .ini 设置吗?请帮我。
编辑:我尝试执行 var_dump ,它确实显示了我的值包含引号:
array(9) {
[0]=>
string(15) ""Total""
[1]=>
string(27) ""Shazam (DE)""
[2]=>
string(15) ""Total""
[3]=>
string(1) ""
...
}
但甚至
$values[0] == "\"Total\""
不起作用。正确的比较器应该是什么?
更新:经过几个小时漫无目的的试验和错误,终于发现了一些烦人的东西,但它解决了我的问题。
最初,当我尝试打开 csv 文件时,它打开了一个记事本,其内容为 ÿþD"" 。我所做的是复制整个文件内容并创建一个同名的新 csv 文件,令我惊讶的是,它成功了。
旧文件在 MS-EXCEL/NOTEPAD 中可以很好地打开,但在 Excel 中,每一行都显示在一个单元格内。
我的问题是,.csv fromat 有什么问题?以前有人遇到过这样的问题吗?
I am trying to parse a csv file where my data is given in a format like :
"Total","Shazam (DE)","Total",,"215,611","538","£935.97","£60.77"
Now if I give fgetcsv() like the one below :
$values = fgetcsv($f,8098);
It returns me an array as :
array(9) {
[0]=>
string(15) ""Total""
[1]=>
string(27) ""Shazam (DE)""
[2]=>
string(15) ""Total""
[3]=>
string(1) ""
[4]=>
string(9) ""215"
[5]=>
string(9) "611""
[6]=>
string(11) ""538""
[7]=>
string(19) ""£935.97""
[8]=>
string(19) ""£60.77"
"
}
but now , If I give a if statement like :
$values = fgetcsv($f,8098);
if ($values[0] == "Total")
echo "Hello";
it's not echoing out anything , why the condition isn't satisfied ? Is it something to do with the enclosing quotes ?
I even tried escaping quotes e.g .
if ($values[0] == '\"Total\"')
echo "Hello";
but to no use.
Do , I need to change any .ini settings for this ? Please help me.
EDIT : I tried doing var_dump , and it did show my values containg quotes :
array(9) {
[0]=>
string(15) ""Total""
[1]=>
string(27) ""Shazam (DE)""
[2]=>
string(15) ""Total""
[3]=>
string(1) ""
...
}
but even
$values[0] == "\"Total\""
didn't work . What should be the right comparator ?
UPDATE : After few hours of aimless trial and error , finally found something annoying , but it fixed my problem .
Initially , when I tried to open my csv file , it opened a notepad with ÿþD"" as it's content . What , I did is , copied the entire filecontent and created a new csv file with the same name , and to my surprise , it worked .
The old file was opening nicely in MS-EXCEL / NOTEPAD , but in excel , each line was being displayed inside one single cell .
My question is , what was the issue with the .csv fromat ? Has anyone coome across with such problem before?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您需要定义一个文本限定符,这在 fgetcsv 中称为封装。
例如:
这是告诉解析器不应该在文本区域内使用逗号。
您应该能够执行以下操作:
更新:
尝试打开记事本,注意当您单击“另存为”时,您可以在底部选择编码,如果您想确保就是这样,请尝试使用不同编码的几个不同文件?
I think you need to define a text qualifier, this is called enclosure in fgetcsv.
eg:
This is to tell the parser that it shouldn't take the comma inside the text area.
You should be able to do:
UPDATE:
try opening notepad and notice when you click save as, you can choose encoding in the buttom, try with a few different files with different encoding if you want to make sure that was it?