scriptwindow__INITIAL_STATE__ = json数据scriptPHP如何匹配网页中以上内容中window__INITIAL_STATE__ = 后带的json数据?
可以使用正则表达式来匹配网页中的该部分内容。以下是一个示例代码,可以匹配出json数据:
<?php
$html = '<script>window.__INITIAL_STATE__ = {json数据}</script>';
$pattern = '/<script>window\.__INITIAL_STATE__ = (\{.*?\})<\/script>/s';
preg_match($pattern, $html, $matches);
$jsonData = $matches[1];
echo $jsonData;
?>
在上述代码中,使用了正则表达式/<script>window\.__INITIAL_STATE__ = (\{.*?\})<\/script>/s,其中:
<script>window\.__INITIAL_STATE__ =:匹配<script>window.__INITIAL_STATE__ =部分;(\{.*?\}):匹配{}中的json数据并保存在捕获组中;<\/script>:匹配</script>部分。
然后使用preg_match()函数进行匹配,并将匹配的结果保存在$matches数组中。最后,通过$matches[1]获取到捕获组中的json数据。
注意:上述代码只适用于简单的情况,如果网页中有多个类似的script标签,可能需要根据实际情况进行调整。
原文地址: https://www.cveoy.top/t/topic/i5cJ 著作权归作者所有。请勿转载和采集!