我正在使用 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
或空字符串 ''
作为 str_getcsv
(附件)的第三个参数,但都不起作用? !?!
我唯一可以用来让 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,或者一个空白空间作为外壳,其次,有没有更好的方法来处理/这样做?
最佳答案
只是给出一个起点......
您可能想考虑使用字符串本身,而不是在您的情况下使用像 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.
}
否则,这只是您的起点,开始并根据您的个人情况进行调整,希望对您有所帮助。
关于php str_getcsv 在制表符分隔列表上中断,没有附件和单独的双引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20022156/