PHP - 如何通过流 url 的公共部分匹配来自两个 m3u8 播放列表的流?

我有两个 m3u8 播放列表,其中包含许多我试图匹配在一起的流。来自 Playlist01 的每个链接在第二个链接中都有它的一对 - Playlist02 并且每一对都有一个公共频道 ID,我想通过它进行匹配。Playlist01 包含如下链接:


#EXTM3U

#EXTINF:-1 tvg-id="Name01" tvg-logo="http://link.png" tvg-chno="100" group-titles="groupone", Name01

path/playlist.php?ch=5101-ab

#EXTINF:-1 tvg-id="Name02" tvg-logo="http://link.png" tvg-chno="160" group-titles="groupone", Name02

path/playlist.php?ch=5102-ab

#EXTINF:-1 tvg-id="Name03" tvg-logo="http://link.png" tvg-chno="650" group-titles="groupone", Name03

path/playlist.php?ch=6234-ab

然后是带有链接的 Playlist02,例如:


#EXTM3U

#EXTINF:-1 tvg-id="Name01" tvg-logo="http://link.png" tvg-chno="100" group-titles="groupone", Name01

http://link345/at/a31350ff5/857183/5101-ab.m3u8

#EXTINF:-1 tvg-id="Name02" tvg-logo="http://link.png" tvg-chno="160" group-titles="groupone", Name02

http://link574/at/bn350frf5/675494/5102-ab.m3u8

#EXTINF:-1 tvg-id="Name03" tvg-logo="http://link.png" tvg-chno="650" group-titles="groupone", Name03

http://link674/at/g4K7J7h0r/845987/6234-ab.m3u8

当然还有一些 Playlist.php,它可以帮助我将来自两个播放列表的链接配对在一起。在 Playlist.php 中,我从 Playlist02 读取数据


<?php

$string = file_get_contents('playlist02.m3u8'); // reading data from playlist02

preg_match_all('/(?P<tag>#EXTINF:-1)|(?<link>http[^\s]+)/', $string, $match ); // parsing data – I want to get just the url links


$count = count( $match[0] );


$result = [];

$index = -1;


for( $i =0; $i < $count; $i++ ){

    $item = $match[0][$i];


    if( !empty($match['tag'][$i])){

        //is a tag increment the result index

        ++$index;

    }elseif( !empty($match['link'][$i])){

        $result[$index]['link'] = $item ;

    }

}



print_r( $result );


呼唤远方
浏览 107回答 1
1回答

慕姐8265434

如果您已经拥有来自 GET 参数的频道,并且您想要将其链接到数组中的一个 url,您可以使用preg_grep。您可以缩短正则表达式以从 playlist02 匹配 .m3u8 结尾的 url 获取链接(?<link>http\S+\.m3u8)然后使用以.m3u8结尾的模式中的 $channel 在 playlist01 的 url 列表中搜索,就像断言字符串结尾的$channel\.m3u8$地方一样。$示例代码$playlist02 = file_get_contents('playlist02.m3u8');preg_match_all('/(?<link>http\S+\.m3u8)/', $playlist02, $matches);$channel = preg_quote("5101-ab");$pattern = "/$channel\.m3u8$/";$result = preg_grep($pattern, $matches["link"]);var_export($result);输出array (  0 => 'http://link345/at/a31350ff5/857183/5101-ab.m3u8',)
打开App,查看更多内容
随时随地看视频慕课网APP