我想在simple HTML Dom
的帮助下显示json脚本的内容,我的目标是显示第二个"@type": "user"
名称和url提要:
我的儿子:
<script type="application/ld+json">
"type": {
"@type": "Type",
"name": "admin"
},
"offers": {
"@type": "AggregateOffer",
"offerCount": "30"
,"offers": [
{
"@type": "user",
"name": "abc",
"url": "https://test.com",
},{
"@type": "user",
"name": "eds",
"url": "https://example.com",
},{
"@type": "user",
"name": "gfh",
"url": "https://test.com",
},{
"@type": "user",
"name": "dfc",
"url": "https://test.com",
},
.
.
.
我想要的输出:"name":"eds";,"url":"https://example.com">
为此,我使用以下代码:
$json = $html->find('script[type="application/ld+json"]',0);
echo $json->innertext;
但这段代码向我展示了所有的json内容,我真的不知道如何在我的代码中设置一个条件,即只有这个复杂循环中的第二个"@type": "user"
名称和url提要会显示给我
假设HTML和JSON有效:
$html = '
<body>
<h1>hello </h1>
<script type="application/ld+json">{
"type": {
"@type": "Type",
"name": "admin"
},
"offers": {
"@type": "AggregateOffer",
"offerCount": "30"
,"offers": [
{
"@type": "user",
"name": "abc",
"url": "https://test.com"
},{
"@type": "user",
"name": "eds",
"url": "https://example.com"
},{
"@type": "user",
"name": "gfh",
"url": "https://test.com"
},{
"@type": "user",
"name": "dfc",
"url": "https://test.com"
}
]
}
}
</script>';
$dom = new DomDocument();
$dom->loadHTML($html);
$node= $dom->getElementsByTagName("script")[1];
$text= $node->textContent;
$obj = json_decode($text, true);
echo $obj["offers"]["offers"][1]["name"];
echo $obj["offers"]["offers"][1]["url"];
UPDATE:页面中还有其他错误,导致解析器无法工作。我建议使用一个函数来提取脚本标记文本。
function after($this1, $inthat)
{
if (!is_bool(strpos($inthat, $this1))) {
return substr($inthat, strpos($inthat, $this1) + strlen($this1));
}
return null;
}
function before($this1, $inthat)
{
return substr($inthat, 0, strpos($inthat, $this1));
}
function between($this1, $that, $inthat)
{
return before($that, after($this1, $inthat));
}
$text = between('<script type="application/ld+json">', '</script>', $html);
// then continue like before.