首页 > 解决方案 > 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 );

输出是:

Array 
( 
[0] => Array ( [link] => http://link345/at/a31350ff5/857183/5101-ab.m3u8 ) 
[1] => Array ( [link] => http://link574/at/bn350frf5/675494/5102-ab.m3u8 ) 
[2] => Array ( [link] => http://link674/at/g4K7J7h0r/845987/6234-ab.m3u8) 
)

从 Playlist01 我得到频道 ID 的方法:

$channel = $_GET["ch"]; // in "ch" I have channel ID e.g. 5101-ab
$link = $result[$index]['link'] = $item ; // this is full link from playlist02 e.g. http://link345/at/a31350ff5/857183/5101-ab.m3u8
$prefix = substr($link,0,88); // this slice the first part of the link http://link345/at/a31350ff5/857183/ (links are originally much longer, so the numbers (0,88) don’t match this example, I just wanted to shorten them for an illustration..)
$chid = substr($link,88,10); // this slice the channel ID from the link „5101-ab“

你能帮我吗,我应该使用什么命令将 Playlist02 中的正确$prefix匹配到 Playlist01 中的["ch"]根据$chid,因为这些参数对于两个播放列表都是通用的?也许我的这种配对逻辑不正确,我应该选择另一种方法,但我是 PHP 初学者,我试图从这个论坛、PHP 手册收集信息,将它们混合并构建一个新的工作脚本。这对只是我缺少的最后一块,不能自己做。最后,我想构建将发送到 Playlist01 的 url,例如:

$url = ($prefix.$channel.".m3u8"); // where $channel from playlist01 should belong to correct $prefix from playlist02 - in this case ch=5101-ab, so ith should have had a prefix which stand in [0] => Array
header('Location: '.$url.'');
exit;
?>

对于您正确定义控制结构(for、foreach)的帮助,我将非常感激,这将帮助我完成这个脚本。非常感谢您的建议!

标签: phpregexmatch

解决方案


如果您已经拥有来自 GET 参数的通道,并且希望将其链接到数组中的一个 url,则可以使用preg_grep

您可以缩短正则表达式以从 playlist02 中获取与 .m3u8 结尾匹配的 url 的链接

(?<link>http\S+\.m3u8)

正则表达式演示

然后使用 $channel 在 playlist01 的 url 列表中搜索以.m3u8结尾的模式,就像断言字符串结尾的$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',
)

推荐阅读