3

str_getcsv用来解析从 nosql 查询返回的制表符分隔值但是我遇到了一个问题,我发现的唯一解决方案是不合逻辑的。

这是一些要演示的示例代码(仅供参考,在此处显示时似乎没有保留选项卡)...

$data = '0  16  Gruesome Public Executions In North Korea - 80 Killed       http://www.youtube.com/watch?v=Dtx30AQpcjw&feature=youtube_gdata        "North Korea staged gruesome public executions of 80 people this month, some for offenses as minor as watching South Korean entertainment videos or being fou...    1384357511  http://gdata.youtube.com/feeds/api/videos/Dtx30AQpcjw   0   The Young Turks                 1   2013-11-13 12:53:31 9ab8f5607183ed258f4f98bb80f947b4    35afc4001e1a50fb463dac32de1d19e7';

$data = str_getcsv($data,"\t",NULL);

echo '<pre>'.print_r($data,TRUE).'</pre>';

特别注意一列(以“朝鲜......”开头的事实实际上以双引号开头,"但不以双引号结束。这就是为什么我提供NULL作为第三个参数(附件)来覆盖默认值"外壳值。

结果如下:

Array
(
[0] => 0
[1] => 16
[2] => Gruesome Public Executions In North Korea - 80 Killed
[3] => http://www.youtube.com/watch?v=Dtx30AQpcjw&feature=youtube_gdata
[4] => 
[5] => North Korea staged gruesome public executions of 80 people this month, some for offenses as minor as watching South Korean entertainment videos or being fou...  1384357511  http://gdata.youtube.com/feeds/api/videos/Dtx30AQpcjw   0   The Young Turks                 1   2013-11-13 12:53:31 9ab8f5607183ed258f4f98bb80f947b4    35afc4001e1a50fb463dac32de1d19e7
)

如您所见,报价正在破坏功能。从逻辑上讲,我认为我可以使用NULLor 和空字符串''作为str_getcsv(enclosure) 的第三个参数,但都没有用?!?!

我唯一可以str_getcsv用来正常工作的是空格字符' '。这对我来说没有任何意义,因为没有一列有空格开始和/或结束它们。

$data = '0  16  Gruesome Public Executions In North Korea - 80 Killed       http://www.youtube.com/watch?v=Dtx30AQpcjw&feature=youtube_gdata        "North Korea staged gruesome public executions of 80 people this month, some for offenses as minor as watching South Korean entertainment videos or being fou...    1384357511  http://gdata.youtube.com/feeds/api/videos/Dtx30AQpcjw   0   The Young Turks                 1   2013-11-13 12:53:31 9ab8f5607183ed258f4f98bb80f947b4    35afc4001e1a50fb463dac32de1d19e7';

$data = str_getcsv($data,"\t",' ');

echo '<pre>'.print_r($data,TRUE).'</pre>';

现在结果是:

Array
(
[0] => 0
[1] => 16
[2] => Gruesome Public Executions In North Korea - 80 Killed
[3] => http://www.youtube.com/watch?v=Dtx30AQpcjw&feature=youtube_gdata
[4] => 
[5] => "North Korea staged gruesome public executions of 80 people this month, some for offenses as minor as watching South Korean entertainment videos or being fou...
[6] => 1384357511
[7] => http://gdata.youtube.com/feeds/api/videos/Dtx30AQpcjw
[8] => 0
[9] => The Young Turks
[10] => 
[11] => 
[12] => 
[13] => 
[14] => 1
[15] => 2013-11-13 12:53:31
[16] => 9ab8f5607183ed258f4f98bb80f947b4
[17] => 35afc4001e1a50fb463dac32de1d19e7
)

所以我的问题是,为什么它可以使用空格作为外壳,而不是 NULL 或空字符串?这也有影响吗?

更新 1:这似乎减少了我在日志中收到的错误数量,但并没有消除它们,所以我猜测我用作外壳的错误造成了意想不到的副作用,尽管比之前的问题更麻烦. 但是我的问题还是一样,为什么我不能使用 NULL 或空白空间作为外壳,其次,有没有更好的方法来处理/执行此操作?

4

2 回答 2

3

只是给一个起点...

str_getcsv您可能想考虑使用字符串本身,而不是像您的情况那样使用函数。

但是请注意,如果您选择这条路线,至少会有一些陷阱(尽管可能是您唯一的选择):

  • 转义字符的处理
  • 数据中的换行符(不作为分隔符)

如果您知道TABS字符串中除了结束字段之外没有任何其他内容,并且除了分隔行之外没有任何换行符,那么您可能会很好:

$data = explode("\n", $the_whole_csv_string_block);

foreach ($data as $line)
{
    $arr = explode("\t", $line);

    // $arr[0] will have every first field of every row, $arr[1] the 2nd, ...
    // Usually this is what I want when working with a csv file

    // But if you rather want a multidimensional array, you can simply add 
    // $arr to a different array and after this loop you are good to go.
}

否则,这只是您的起点,开始并根据您的个人情况进行调整,希望对您有所帮助。

于 2014-08-26T11:38:07.807 回答
2

只需chr(0)用作外壳和逃生:

$data = str_getcsv($data, "\t", chr(0), chr(0));
于 2018-12-29T07:27:03.800 回答