PHP 代码正则匹配解析:提取 JavaScript 变量 player_aaaa 的值
这段代码的正则表达式匹配的内容是正确的,可以提取出变量 $html 中的 player_aaaa 对应的值。代码使用 preg_match 函数匹配 var player_aaaa=(.*?); 的模式,其中 (.*?) 表示匹配任意字符,但尽量少的匹配,最终获取到 player_aaaa 对应的值。
代码示例:
<?php
$html = '<script type="text/javascript">var player_aaaa = {"flag":"play","encrypt":0,"trysee":0,"points":0,"link":"\/vodplay\/339927-1-1.html","link_next":"","link_pre":"","vod_data":{"vod_name":"\u9053\u58eb\u964d\u9b542\u4e4b\u50f5\u5c38\u6751","vod_actor":"\u6768\u6613\u4e4b,\u9648\u8bd7\u5609,\u8fb0\u8f69\u9510","vod_director":"\u6768\u6613\u4e4b","vod_class":"\u6050\u6016,\u5267\u60c5"},"url":"https:\/\/vip.lz-cdn7.com\/20230118\/13580_14fd770e\/index.m3u8","url_next":"","from":"lzm3u8","server":"no","note":"","id":"339927","sid":1,"nid":1};</script><script type="text/javascript" src="\/static\/js\/playerconfig.js?t=20230527"></script><script type="text/javascript" src="\/static\/js\/player.js?t=a20230527"></script>';
preg_match('/var player_aaaa=(.*?);/', $html, $match);
if(isset($match[1])) {
$player_data = json_decode($match[1], true);
// 输出所有键值内容
print_r($player_data);
}
?>
解析:
- 代码首先定义一个包含 JavaScript 代码的字符串变量
$html。 preg_match函数使用正则表达式'/var player_aaaa=(.*?);/'匹配$html中的内容。var player_aaaa=匹配字面量var player_aaaa=。(.*?)匹配任意字符,使用?使匹配尽量少,避免匹配到其他var变量。;匹配分号,表示 JavaScript 代码块结束。
- 匹配成功后,
$match[1]保存了匹配到的player_aaaa对应的 JSON 字符串。 json_decode函数将 JSON 字符串解析为数组,并将结果存储到$player_data变量中。- 最后,使用
print_r函数输出$player_data的所有键值对内容。
如果代码无法提取出你想要的值,请提供更详细的信息和问题描述,例如你期望获取的内容、实际获取的内容等等,以便更好地帮助你解决问题。
原文地址: https://www.cveoy.top/t/topic/ojg5 著作权归作者所有。请勿转载和采集!