php';s str_getcsv在选项卡分隔列表上断开,没有附件和单独的双引号
我正在使用php';s str_getcsv在选项卡分隔列表上断开,没有附件和单独的双引号,php,csv,nosql,tsv,handlersocket,Php,Csv,Nosql,Tsv,Handlersocket,我正在使用str_getcsv解析从nosql查询返回的以制表符分隔的值,但是我遇到了一个问题,我找到的唯一解决方案是不合逻辑的 这里有一些示例代码要演示(仅供参考,在这里显示选项卡时似乎没有保留) 正如您所看到的,引号正在破坏函数。从逻辑上讲,我认为我可以使用NULL或和空字符串'作为str_getcsv(附件)的第三个参数,但两者都不起作用 我唯一能让str_getcsv正常工作的方法就是使用空格字符'。这对我来说没有任何意义,因为没有一列以空格开头和/或结尾 Array ( [0] =&g
str_getcsv
解析从nosql查询返回的以制表符分隔的值,但是我遇到了一个问题,我找到的唯一解决方案是不合逻辑的
这里有一些示例代码要演示(仅供参考,在这里显示选项卡时似乎没有保留)
正如您所看到的,引号正在破坏函数。从逻辑上讲,我认为我可以使用NULL
或和空字符串'
作为str_getcsv
(附件)的第三个参数,但两者都不起作用
我唯一能让str_getcsv
正常工作的方法就是使用空格字符'
。这对我来说没有任何意义,因为没有一列以空格开头和/或结尾
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,或者一个空的空间作为存储模块,其次,有没有更好的方法来处理这个问题呢?只是给一个起点
您可能想考虑使用字符串本身,而不是使用像
- 转义字符的处理
- 数据中的换行符(不是分隔符)
选项卡,并且除了分隔行的选项卡之外没有任何换行符,那么您可以这样做:
否则,这只是您的一个起点,要开始并调整它以适应您的个人情况,希望它有所帮助。只需使用chr(0)
作为附件和转义:
这是一个很好的观点,有时内置函数的范围有点有限,无法处理所有可能发生的情况。我会考虑编写我自己的解析器,谢谢你的建议和示例。这对于在批处理模式下使用<代码> MySQL < /Cule> CLI非常有用。
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
)
$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
)
$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.
}
$data = str_getcsv($data, "\t", chr(0), chr(0));