str_getcsv不能正确解析数据 [英] str_getcsv not parsing the data correctly
问题描述
我有一个问题str_getcsv函数的PHP。
I have a problem with str_getcsv function for PHP.
我有这个代码:
<?php
$string = '#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=714000,RESOLUTION=640x480,CODECS="avc1.77.30, mp4a.40.34"';
$array = str_getcsv($string, ",", '"');
print_r($array);
应该返回:
Array
(
[0] => #EXT-X-STREAM-INF:PROGRAM-ID=1
[1] => BANDWIDTH=714000
[2] => RESOLUTION=640x480
[3] => CODECS=avc1.77.30, mp4a.40.34
)
但是,它返回:
Array
(
[0] => #EXT-X-STREAM-INF:PROGRAM-ID=1
[1] => BANDWIDTH=714000
[2] => RESOLUTION=640x480
[3] => CODECS="avc1.77.30
[4] => mp4a.40.34"
)
b $ b
因为它忽略了最后一个参数:CODECS的外壳,并且正在分裂这个信息我使用str_getcsv而不是恰恰做爆炸(,,$ string)
Cause it is ignoring the enclosure of the last parameter: CODECS and is spliting also that information. I'm using str_getcsv instead of just doing explode(",", $string) precisely for that reason (that function should respect the enclosure) but it is working the same as explode will do it.
正在执行的代码: http://eval.in/17471
推荐答案
机箱(第三个)参数没有相同的效果。
The enclosure (third) parameter does not have quite that effect. The enclosure character is treated as such only when it appears next to the delimiter.
要获得所需的输出,输入将需要
To get your desired output, the input would need to be
#EXT-X-STREAM-INF:PROGRAM-ID=1,...,"CODECS=avc1.77.30, mp4a.40.34"
查看操作 。
See it in action.
这篇关于str_getcsv不能正确解析数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!