使用 fgetcsv() 解析 csv(用引号引起来的数据)无法正常工作

发布于 2024-12-18 03:52:25 字数 1501 浏览 0 评论 0原文

我正在尝试解析一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沙与沫 2024-12-25 03:52:25

我认为您需要定义一个文本限定符,这在 fgetcsv 中称为封装。
例如:

$values = fgetcsv($f,8098,'"'); 
// the last part is: singlequote doublequote singlequote,
// you could also use an escape "\"" 

这是告诉解析器不应该在文本区域内使用逗号。

您应该能够执行以下操作:

$values[0] == '"Total"'

更新:

您的文件打开问题很奇怪,可能与
文件编码,通过创建一个新文件,您使用新的文件创建了它
编码可能是 ANSI/UTF-8/Unicode 原始文件不是的,然后将内容复制到那里,所以现在它以该格式保存。我不确定为什么这会给你遇到的问题,但我假设解析器无法处理错误的编码。

尝试打开记事本,注意当您单击“另存为”时,您可以在底部选择编码,如果您想确保就是这样,请尝试使用不同编码的几个不同文件?

I think you need to define a text qualifier, this is called enclosure in fgetcsv.
eg:

$values = fgetcsv($f,8098,'"'); 
// the last part is: singlequote doublequote singlequote,
// you could also use an escape "\"" 

This is to tell the parser that it shouldn't take the comma inside the text area.

You should be able to do:

$values[0] == '"Total"'

UPDATE:

You problem with the file opening oddly, is probably related to
the files encoding, by creating a new file, you created it with a new
encoding maybe ANSI/UTF-8/Unicode something the original file wasnt, and then copied the content in there, so now it was saved in that format instead.. im not sure why this would give you the problems you are experiencing, but im assume the parser somehow cant handle the wrong encoding.

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文