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

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

'.print_r($data,TRUE).'
';

请特别注意以下事实:一列(以“ 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 '

'.print_r($data,TRUE).'
';

现在的结果是:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值