PHP - how to match streams from two m3u8 playlists through a common part of the stream url?

2020-05-27 php regex match

I have two m3u8 playlists containing dozens of streams I tried to match together. Every link from the Playlist01 have its pair in the second one – Playlist02 and each pair have a common channel ID through which I would like to do this match. Playlist01 contains links as:

#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

Then there is Playlist02 with links such as:

#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

And of course there is some Playlist.php, which should help me pair links from both playlists together. In Playlist.php I read data from 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 );

Output is:

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

From Playlist01 I get channel ID with method:

$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“

Could you please help me, what commands should I use to match correct $prefix from the Playlist02 to ["ch"] from Playlist01 according to the $chid, as these parameters are common for both playlists? Maybe my logic of this pairing is not correct and I should choose another approach, but I am PHP beginner and I tried to collect info from this forum, PHP manuals, blend them and build a new working script. This pairing is just the last piece I am missing and can’t do by myself. In the end I would like to build and url that will be sent to Playlist01, such as:

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

I would be really gratefull for your help with correct definition of a control structure (for, foreach) that will help me to finish this script. Many thanks for your advice!

Answers

If you already have the channel from a GET parameter, and you want to link it to one of the urls from the array, you might use preg_grep.

You could shorten the regex to get the links from playlist02 matching urls the end on .m3u8

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

Regex demo

Then search in the list of urls from playlist01 using the $channel in a pattern that ends on .m3u8 like $channel\.m3u8$ where $ asserts the end of the string.

Example code

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

Output

array (
  0 => 'http://link345/at/a31350ff5/857183/5101-ab.m3u8',
)

Related