|
| 1 | +<?php |
| 2 | +$http_user_agent = $_SERVER['HTTP_USER_AGENT']??'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'; |
| 3 | +if ($_SERVER["REQUEST_METHOD"] == "POST") { |
| 4 | + $req_url = $_POST["user_path_1"]??''; |
| 5 | + $list_data = $_POST["list_data"]??''; |
| 6 | + $scheme = $_POST["scheme"]??''; |
| 7 | + if ($req_url){ |
| 8 | + $userAgent = $_POST["user_path_2"]??'$http_user_agent'; |
| 9 | + $referer = $_POST["user_path_3"]??''; |
| 10 | + if ($referer){ |
| 11 | + preg_match('/https?:\/\/[^\/\n]+/',$referer,$match_referer); |
| 12 | + $origin = $match_referer[0]; |
| 13 | + } else { |
| 14 | + preg_match('/https?:\/\/[^\/\n]+/',$req_url,$match_req_url); |
| 15 | + $referer = $origin = $match_req_url[0]; |
| 16 | + } |
| 17 | + $headers = [ |
| 18 | + "User-Agent: $userAgent", |
| 19 | + "Referer: $referer", |
| 20 | + "Origin: $origin", |
| 21 | + "Accept-Language: en-US,en;q=0.9" |
| 22 | + ]; |
| 23 | + $ch = curl_init($req_url); |
| 24 | + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); |
| 25 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); |
| 26 | + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); |
| 27 | + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); |
| 28 | + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
| 29 | + $response = curl_exec($ch); |
| 30 | + curl_close($ch); |
| 31 | + if (curl_error($ch)) { |
| 32 | + echo 'CURL请求失败'.PHP_EOL.$errorMessage; |
| 33 | + exit; |
| 34 | + } |
| 35 | + if ($response){ |
| 36 | + $list_data .= PHP_EOL.$response; |
| 37 | + } |
| 38 | + } |
| 39 | + if ($list_data){ |
| 40 | + #初步格式化m3u列表 |
| 41 | + $list_data = preg_replace('/\n+/',"\n",$list_data);//去除连续空白行 |
| 42 | + $list_data = preg_replace('/#+/',"#",$list_data);//去除连续# |
| 43 | + if (strpos($list_data,'#EXTM3U')!==false){ |
| 44 | + $list_data = str_replace("#\n#","\n#",$list_data);//去除行尾# |
| 45 | + $list_data = preg_replace('/([^\n])#EXTINF/',"$1\n#EXTINF",$list_data);//#EXTINF自动换行 |
| 46 | + #以下实现格式化m3u中的链接 |
| 47 | + if (preg_match_all('/.+#.+/',$list_data,$match_urls)){//捕获所有需要格式化的项目 |
| 48 | + foreach($match_urls[0] as $match_url){//对于匹配到的每一项,统计#的个数 |
| 49 | + preg_match_all('/#/',$match_url,$match_count); |
| 50 | + $counts[] = count($match_count[0]); |
| 51 | + } |
| 52 | + $max_count = max($counts);//取含#个数的最大值 |
| 53 | + for ($i=0;$i<$max_count;$i++){ |
| 54 | + $list_data = preg_replace('/(#EXTINF.+)\n([^#\n]+)#(.+)/',"$1\n$2\n$1\n$3",$list_data);//格式化m3u |
| 55 | + } |
| 56 | + } |
| 57 | + #以下实现url去重及转换 |
| 58 | + preg_match_all('/#EXTINF.+group-title="(.+)",(.+)\n([^\n#]+)/',$list_data,$matches); |
| 59 | + $result = $matches[1][0].',#genre#'.PHP_EOL.$matches[2][0].','.$matches[3][0].PHP_EOL; |
| 60 | + for ($i=1;$i<count($matches[1]);$i++){ |
| 61 | + if (strpos($result,$matches[3][$i])){//url已存在则去重 |
| 62 | + continue; |
| 63 | + } else { |
| 64 | + $group = ($matches[1][$i] == $matches[1][$i-1])?'':$matches[1][$i].',#genre#'.PHP_EOL;//分组处理 |
| 65 | + $result .= $group.$matches[2][$i].','.$matches[3][$i].PHP_EOL; |
| 66 | + } |
| 67 | + } |
| 68 | + #分类去重 |
| 69 | + if ($scheme){ |
| 70 | + $result = preg_replace('/.*#genre#\n/','',$result); |
| 71 | + } else { |
| 72 | + $result = txt_classification_oneness($result); |
| 73 | + } |
| 74 | + echo $result; |
| 75 | + } else if (strpos($list_data,'#genre#')!==false){ |
| 76 | + $list_data = str_replace("\n#","\n",$list_data);//去除行首# |
| 77 | + $list_data = preg_replace('/([^e])#\n/',"$1\n",$list_data);//去除行尾# |
| 78 | + #格式化txt |
| 79 | + if (preg_match_all('/.+,.+#.+/',$list_data,$match_urls)){ |
| 80 | + for ($i=0;$i<count($match_urls[0]);$i++){ |
| 81 | + preg_match_all('/#/',$match_urls[0][$i],$match_count); |
| 82 | + $counts[] = count($match_count[0]); |
| 83 | + } |
| 84 | + $max_count = max($counts); |
| 85 | + for ($i=0;$i<$max_count;$i++){ |
| 86 | + $list_data = preg_replace('/(.+,)(.+)#(.+)/',"$1$2\n$1$3",$list_data); |
| 87 | + } |
| 88 | + } |
| 89 | + #分类去重 |
| 90 | + $list_data = txt_classification_oneness($list_data); |
| 91 | + #以下实现url去重及转换 |
| 92 | + preg_match_all('/(.+),(.+)/',$list_data,$matches); |
| 93 | + $result = '#EXTM3U'.PHP_EOL; |
| 94 | + for($i=0;$i<count($matches[0]);$i++){ |
| 95 | + if ($matches[2][$i] == '#genre#'){ |
| 96 | + $classification = $matches[1][$i]; |
| 97 | + } else if (strpos($result,$matches[2][$i])){ |
| 98 | + continue; |
| 99 | + } else { |
| 100 | + $result .= '#EXTINF:-1 group-title="'.$classification.'",'.$matches[1][$i].PHP_EOL.$matches[2][$i].PHP_EOL; |
| 101 | + } |
| 102 | + } |
| 103 | + echo $result; |
| 104 | + } else { |
| 105 | + die("Your file isn't the M3U or txt format list."); |
| 106 | + } |
| 107 | + } else { |
| 108 | + die('Please input your list link or data.'); |
| 109 | + } |
| 110 | + exit; |
| 111 | +} |
| 112 | +#以下实现txt格式下的分类去重 |
| 113 | +function txt_classification_oneness($txt){ |
| 114 | + preg_match_all('/.*,#genre#\n|(?:.*,[^#\n]+\n)+/',$txt,$matches_txt);//匹配每个分类或分类段 |
| 115 | + $counts_txt = count($matches_txt[0]); |
| 116 | + for ($i=2;$i<$counts_txt-1;$i+=2){//遍历每个分类 |
| 117 | + for ($j=0;$j<$i;$j+=2){//检查前面是否已经出现过 |
| 118 | + if ($matches_txt[0][$j]==$matches_txt[0][$i]){ |
| 119 | + $b = $matches_txt[0][$i+1]; |
| 120 | + for ($k=$i;$k>$j+2;$k--){ |
| 121 | + $matches_txt[0][$k + 1] = $matches_txt[0][$k - 1]; |
| 122 | + } |
| 123 | + $matches_txt[0][$k] = ''; |
| 124 | + $matches_txt[0][$k+1] = $b; |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + $result = ''; |
| 129 | + for ($i=0;$i<$counts_txt;$i++){ |
| 130 | + if($matches_txt[0][$i]){ |
| 131 | + $result .= $matches_txt[0][$i]; |
| 132 | + } |
| 133 | + } |
| 134 | + return $result; |
| 135 | +} |
| 136 | +?> |
0 commit comments