问题
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