Extract JSON object from html using PHP regex

耗尽温柔 提交于 2021-02-07 18:12:51

问题


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.


回答1:


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/



来源:https://stackoverflow.com/questions/45879184/extract-json-object-from-html-using-php-regex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!