title: PHP - how to match streams from two m3u8 playlists through a common part of the stream url?
body: <p>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:</p>
  
  <pre><code>#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
  </code></pre>
  
  <p>Then there is Playlist02 with links such as:</p>
  
  <pre><code>#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
  </code></pre>
  
  <p>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 </p>
  
  <pre><code>&lt;?php
  $string = file_get_contents('playlist02.m3u8'); // reading data from playlist02
  preg_match_all('/(?P&lt;tag&gt;#EXTINF:-1)|(?&lt;link&gt;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 &lt; $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 );
  </code></pre>
  
  <p>Output is:</p>
  
  <pre><code>Array 
  ( 
  [0] =&gt; Array ( [link] =&gt; http://link345/at/a31350ff5/857183/5101-ab.m3u8 ) 
  [1] =&gt; Array ( [link] =&gt; http://link574/at/bn350frf5/675494/5102-ab.m3u8 ) 
  [2] =&gt; Array ( [link] =&gt; http://link674/at/g4K7J7h0r/845987/6234-ab.m3u8) 
  )
  </code></pre>
  
  <p>From Playlist01 I get channel ID with method:</p>
  
  <pre><code>$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“
  </code></pre>
  
  <p>Could you please help me, what commands should I use to match correct <strong>$prefix</strong> from the Playlist02 to <strong>["ch"]</strong> from Playlist01 according to the <strong>$chid</strong>, 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:</p>
  
  <pre><code>$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] =&gt; Array
  header('Location: '.$url.'');
  exit;
  ?&gt;
  </code></pre>
  
  <p>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!</p>