str_getcsv not parsing the data correctly

ぃ、小莉子 提交于 2019-12-13 16:23:23

问题


I have a problem with str_getcsv function for PHP.

I have this code:

<?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);

Which should return:

Array
(
    [0] => #EXT-X-STREAM-INF:PROGRAM-ID=1
    [1] => BANDWIDTH=714000
    [2] => RESOLUTION=640x480
    [3] => CODECS=avc1.77.30, mp4a.40.34
)

But instead, it is returning:

Array
(
    [0] => #EXT-X-STREAM-INF:PROGRAM-ID=1
    [1] => BANDWIDTH=714000
    [2] => RESOLUTION=640x480
    [3] => CODECS="avc1.77.30
    [4] =>  mp4a.40.34"
)

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.

The code being executed: http://eval.in/17471


回答1:


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.



来源:https://stackoverflow.com/questions/16195128/str-getcsv-not-parsing-the-data-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!