Skip to content
Advertisement

Is there any way to add a starting of link to a unknown m3u8 file

I recently wrote a code which uses file_get_contents along with some USER_AGENT headers and other headers to get a source of a m3u8 file , when echoed it looks like following

#EXTM3U #EXT-X-STREAM-INF:CLOSED-CAPTIONS=NONE,RESOLUTION=1280x720,FRAME-RATE=25.000,CODECS="avc1.4d001f,mp4a.40.2",AVERAGE-BANDWIDTH=1500000,BANDWIDTH=1870000 tracks-v1a1/mono.m3u8?token=XYZ_YIE2kpAL6yriswpbrwGAvA%3D%3D%2C1608554743.5691007667

Now I wrote a code to add http://example.com/mono.php? to the url (tracks-v1a1 one ) The code which I wrote is

$test_value = str_replace('tracks-v1a1/mono.m3u8?token=' . "(*^)", "http://example.com/mono.php/tracks-v1a1/mono.m3u8?token=" . "-1/", $hs2);

I tried using preg_replace too but was unsuccessful, can anyone please find out my mistake.

EDITED According to the The Fourth Bird Sir’s link and code The following code worked like a charm

<?php

$hs2 = '#EXTM3U #EXT-X-STREAM-INF:CLOSED-CAPTIONS=NONE,RESOLUTION=1280x720,FRAME-RATE=25.000,CODECS="avc1.4d001f,mp4a.40.2",AVERAGE-BANDWIDTH=1500000,BANDWIDTH=1870000 tracks-v1a1/mono.m3u8?token=XYZ_YIE2kpAL6yriswpbrwGAvA%3D%3D%2C1608554743.5691007667';
$pattern = "~tracks-v1a1/mono.m3u8?token=S+~";
$replacement = "http://example.com/mono.php?$0";
$test_value = preg_replace($pattern, $replacement, $hs2);

echo $test_value;

Thanks a lot ‘The Fourth Bird ‘ Sir ,

Advertisement

Answer

You could use

$hs2 = '#EXTM3U #EXT-X-STREAM-INF:CLOSED-CAPTIONS=NONE,RESOLUTION=1280x720,FRAME-RATE=25.000,CODECS="avc1.4d001f,mp4a.40.2",AVERAGE-BANDWIDTH=1500000,BANDWIDTH=1870000 tracks-v1a1/mono.m3u8?token=XYZ_YIE2kpAL6yriswpbrwGAvA%3D%3D%2C1608554743.5691007667';
$pattern = "~tracks-v1a1/mono.m3u8?token=S+~";
$replacement = "http://example.com/mono.php?$0";
$test_value = preg_replace($pattern, $replacement, $hs2);

echo $test_value;

Output

#EXTM3U #EXT-X-STREAM-INF:CLOSED-CAPTIONS=NONE,RESOLUTION=1280x720,FRAME-RATE=25.000,CODECS="avc1.4d001f,mp4a.40.2",AVERAGE-BANDWIDTH=1500000,BANDWIDTH=1870000 http://example.com/mono.php?tracks-v1a1/mono.m3u8?token=XYZ_YIE2kpAL6yriswpbrwGAvA%3D%3D%2C1608554743.5691007667

See a php demo

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement