使用PHP正则表达式从HTML提取JSON对象 [英] Extract JSON object from html using PHP regex

查看:117
本文介绍了使用PHP正则表达式从HTML提取JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读所有相关线程后,我找不到任何能显示正则表达式的东西,该正则表达式能够从html内容中提取完整的json对象,因此希望有人可以帮助我获得正确的正则表达式来解决此问题.

例如,要提取的json im如下所示:

 分类法":{页面":"/products/1/",价格":"350.00",国家/地区代码":"gb",品牌":苹果"}, 

我正在尝试提取html中Java脚本函数内部的整个分类"对象.

我尝试过 preg_match('/\ taxonomy \ s * =(.+)(?:; |/',$ file,$ m); ,但没有喜悦和正则表达式我正在尝试学习.

我的目标是让正则表达式解析html并从html中提取分类对象,以便我留下以下内容: {"page":"/products/1/","price":"350.00","country_code":"gb","brand":"apple"} ,然后我就可以对json_decode

如果有人可以帮助我使用正确的正则表达式,我将不胜感激,谢谢.

解决方案

此regex模式应该可以使用,但是取决于您的完整HTML外观是什么

 <?php$ file ='分类法":{页面":"/products/1/",价格":"350.00",国家/地区代码":"gb",品牌":苹果"},';preg_match('@分类法":(.*?)\},@ s',$ file,$ m);if(!empty($ m [1])){$ jsonString ="[".$ m [1].}]";$ array = json_decode($ jsonString,true);print_r($ array);} 

https://regex101.com/r/fytDO8/1/

After reading all related threads i can not find anything that shows regex that is capable of extracting a full json object from within html content so im hoping someone can help me get the right regex to resolve the issue.

For example the json im looking to extract looks like this:

"taxonomy": {"page":"/products/1/","price":"350.00","country_code":"gb","brand":"apple"},

Im trying to extract the entire "taxonomy" object that is inside a java script function within the html.

I have tried preg_match('/\taxonomy\s*=(.+)(?:;|/', $file, $m); but having no joy and regex is something im trying to learn.

Im aiming to have the regex parse the html and pull the taxonmy object from the html so im left with the following: {"page":"/products/1/","price":"350.00","country_code":"gb","brand":"apple"} that i can then json_decode

I would be greatly appreciate if someone could help me get to the correct regex, Thanks.

解决方案

This regex pattern should work, but it depends on what is your full HTML looks like

<?php
$file = '"taxonomy": {"page":"/products/1/","price":"350.00","country_code":"gb","brand":"apple"},
';
preg_match('@"taxonomy":(.*?)\},@s', $file, $m);

if(!empty($m[1])){
    $jsonString = "[".$m[1] . "}]";
    $array = json_decode($jsonString, true);
    print_r($array);
}

https://regex101.com/r/fytDO8/1/

这篇关于使用PHP正则表达式从HTML提取JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆