ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

php的str_getcsv在制表符分隔的列表上中断,没有附件,并且用双引号引起来

2019-10-30 06:41:56  阅读:340  来源: 互联网

标签:csv nosql tsv php handlersocket


我正在使用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>';

请特别注意以下事实:一列(以“ North Korea ….开头”实际上以双引号开头,但没有以双引号结尾.这就是为什么我提供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正常工作,我唯一可以使用的是空格char”.对我而言,这没有任何意义,因为所有列都没有以空格开头和/或结尾.

$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:看来这减少了我在日志中收到的错误数量,但并没有消除它们,因此我猜测用作机箱的I引起了意想不到的副作用,尽管比以前的问题少了麻烦.但是我的问题仍然是一样的,为什么我不能使用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.
}

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

标签:csv,nosql,tsv,php,handlersocket
来源: https://codeday.me/bug/20191030/1965982.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有