forked from onigetoc/m3u8-PHP-Parser
-
Notifications
You must be signed in to change notification settings - Fork 2
/
m3u-parser-simple.php
73 lines (50 loc) · 1.86 KB
/
m3u-parser-simple.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
header('Content-Type: application/json');
$url = $_GET["url"];
if(isset($url)) {
$m3ufile = file_get_contents($url);
} else {
$m3ufile = file_get_contents('http://pastebin.com/raw/t1mBJ2Yi');
}
//$m3ufile = str_replace('tvg-', 'tvg_', $m3ufile);
$m3ufile = str_replace('group-title', 'tvgroup', $m3ufile);
$m3ufile = str_replace("tvg-", "tv", $m3ufile);
//$re = '/#(EXTINF|EXTM3U):(.+?)[,]\s?(.+?)[\r\n]+?((?:https?|rtmp):\/\/(?:\S*?\.\S*?)(?:[\s)\[\]{};"\'<]|\.\s|$))/';
$re = '/#EXTINF:(.+?)[,]\s?(.+?)[\r\n]+?((?:https?|rtmp):\/\/(?:\S*?\.\S*?)(?:[\s)\[\]{};"\'<]|\.\s|$))/';
$attributes = '/([a-zA-Z0-9\-]+?)="([^"]*)"/';
preg_match_all($re, $m3ufile, $matches);
// Print the entire match result
//print_r($matches);
$i = 1;
$items = array();
foreach($matches[0] as $list) {
//echo "$list <br>";
preg_match($re, $list, $matchList);
//$mediaURL = str_replace("\r\n","",$matchList[4]);
//$mediaURL = str_replace("\n","",$matchList[4]);
//$mediaURL = str_replace("\n","",$mediaURL);
$mediaURL = preg_replace("/[\n\r]/","",$matchList[3]);
$mediaURL = preg_replace('/\s+/', '', $mediaURL);
//$mediaURL = preg_replace( "/\r|\n/", "", $matches[4] );
$newdata = array (
//'ATTRIBUTE' => $matchList[2],
'id' => $i++,
'tvtitle' => $matchList[2],
'tvmedia' => $mediaURL
);
preg_match_all($attributes, $list, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$newdata[$match[1]] = $match[2];
}
//array_push($newdata,$attribute);
//$newdata[] = $attribute;
$items[] = $newdata;
//$items[] = $matchList[2];
}
//print_r($items);
$callback= $_GET['callback'];
if($callback)
echo $callback. '(' . json_encode($items) . ')'; // jsonP callback
else
echo json_encode($items);
?>