我有這個 html 文本,需要將 mydata 從 html 提取到 php<script>var mydata=[{"id":105,"i":"4.jpg"},{id":13,"i":"5.jpg"}];//some other js code</script>我在 php 中嘗試過:$html = file_get_contents($url);$js = preg_grep("/var mydata={.*}/", $html);$js = str_replace("var mydata=", "", $js );$sv = json_decode($js);print_r($sv);沒有提取任何內容。請問我做錯了什么?PS我在這里找到了類似的主題How to get Javascript variable from an HTML page?
1 回答

瀟瀟雨雨
TA貢獻1833條經驗 獲得超4個贊
preg_match是解決方案:
$matches = array();
$js= preg_match("/var mydata=\[.*\]/", $html, $matches);
$match = str_replace("var mydata=", "", $matches[0]);
$sv = json_decode($match);
print_r($sv);
- 1 回答
- 0 關注
- 134 瀏覽
添加回答
舉報
0/150
提交
取消