How to retrieve variable=“value” pairs from m3u string

后端 未结 1 541
旧巷少年郎
旧巷少年郎 2021-01-28 23:50

I have m3u file that contain lines like example:

#EXTINF:0 $ExtFilter=\"Viva\" group-title=\"Variedades\" tvg-logo=\"logo/Viva.png\" tvg-name=\"Viva\"

相关标签:
1条回答
  • 2021-01-29 00:42

    Use preg_match_all to perform multiple matches:

    preg_match_all('/([\w-]+)="([\w-\s.\/]+)"/i',$str,$matches, PREG_SET_ORDER);
    

    It returns the results as a 2-dimensional array -- one dimension is the match, another dimension is the capture groups within the matches. To get them into a single array as in your desired result, use a loop:

    $results = array();
    foreach ($matches as $match) {
        array_push($results, $match[1], $match[2]);
    }
    print_r($results);
    

    Prints:

    Array
    (
        [0] => ExtFilter
        [1] => Viva
        [2] => group-title
        [3] => Variedades
        [4] => tvg-logo
        [5] => logo/Viva.png
        [6] => tvg-name
        [7] => Viva
    )
    

    I simplified your regexp by using \w in place of a-z0-9_. I also added / to the second character set, so that logo/Viva.png would match. I got rid of \s+ at the end, because it prevented the last variable assignment from working.

    0 讨论(0)
提交回复
热议问题