window__INITIAL_STATE__ = json数据PHP如何匹配网页中以上内容中window__INITIAL_STATE__ = 后带的json数据?
在PHP中,可以使用正则表达式来匹配网页中的内容,并提取出需要的部分。下面是一个示例代码:
<?php
// 网页内容
$html = '<script>window.__INITIAL_STATE__ = {"key1": "value1", "key2": "value2"};</script>';
// 正则表达式匹配
$pattern = '/window\.__INITIAL_STATE__\s*=\s*({.*?});/s';
preg_match($pattern, $html, $matches);
// 提取json数据
$jsonData = $matches[1];
// 解析json数据
$data = json_decode($jsonData, true);
// 打印结果
print_r($data);
?>
上述代码中,使用正则表达式/window\.__INITIAL_STATE__\s*=\s*({.*?});/s来匹配网页中的window.__INITIAL_STATE__ = {...};这一行,并提取出{...}部分的json数据。然后使用json_decode()函数将json数据解析为PHP数组,最后打印出结果。
注意:上述代码仅适用于提取单个json对象,如果网页中存在多个window.__INITIAL_STATE__的情况,需要进行相应的调整。
原文地址: https://www.cveoy.top/t/topic/i5cL 著作权归作者所有。请勿转载和采集!