I'm not 100% without testing the code, but $jsonScripts
will contain multiple items if there is more than one, and the total number of these can be obtained using the length
property.
To go through them all, use a simple for
loop to obtain each one:
...
$jsonScripts = $xp->query( '//script[@type="application/ld+json"]' );
$json = '';
for ($i = 0; $i < $jsonScripts->length; $i++) {
$json .= trim($jsonScripts->item($i)->nodeValue );
}
...
This will simply concatenate the contents of each script node onto the $json
variable. I'm not certain whether you want to separate them by a delimiter or anything else.
Where your code was previously using:
$json = trim( $jsonScripts->item(0)->nodeValue );
This was obtaining only the first element. The code I suggested uses:
$json .= trim($jsonScripts->item($i)->nodeValue);
This concatenates the $i
th element and should therefore output all of them starting with the 0
th.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…