我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
)
如您所见,报价正在破坏功能。从逻辑上讲,我认为我可以使用NULL
or 和空字符串''
作为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 或空白空间作为外壳,其次,有没有更好的方法来处理/执行此操作?