You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:如何移除PHP中file_get_contents输出链接的');'后缀

解决m3u8链接末尾多余');的问题

你的问题很典型——正则匹配时不小心把目标内容之外的收尾字符也包含进来了,这里有几个简单有效的修复方案:

方法1:直接截取字符串(最直观)

既然你确定末尾固定是');这两个字符,用substr()直接切掉最后两位就行:

<?php 
$streamSite = "http://testsite.com/token.php"; 
function writeFile($url, $channel) { 
    $File = $channel.".strm"; 
    $Handle = fopen($File, 'w'); 
    fwrite($Handle, $url); 
    fclose($Handle); 
} 
$input = @file_get_contents($streamSite) or die("Please try again later: $url"); 
preg_match('/(m3u8.*hdnea=.*acl=.*~hmac=.*)/', $input, $matches);
// 移除末尾的');'
$cleanedUrl = substr($matches[1], 0, -2);
echo $cleanedUrl; 
?>

方法2:优化正则表达式(从根源避免)

调整正则规则,让它只匹配到~hmac=后的有效字符,不包含后续的');:

<?php 
$streamSite = "http://testsite.com/token.php"; 
function writeFile($url, $channel) { 
    $File = $channel.".strm"; 
    $Handle = fopen($File, 'w'); 
    fwrite($Handle, $url); 
    fclose($Handle); 
} 
$input = @file_get_contents($streamSite) or die("Please try again later: $url"); 
// 优化正则,仅匹配到有效链接部分
preg_match('/(m3u8.*hdnea=.*acl=.*~hmac=[^\');]+)/', $input, $matches);
echo $matches[1]; 
?>

方法3:用rtrim()精准移除指定字符

如果不确定末尾字符长度,但明确多余的是');,可以用rtrim()精准清理:

// 在echo前添加这行
$cleanedUrl = rtrim($matches[1], '\');');
echo $cleanedUrl;

任意选一种方法,处理后的m3u8链接就能正常使用了!

内容的提问来源于stack exchange,提问作者Harish nl

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:31:33