问题
I'd like to get all of the content including tags of a parent element, let's say:
$imgs = $xpath->query('//img');
echo $imgs->item(0)->parentNode->innerHTML;
Of course, innerHTML
attribute doesn't exist. The most similar attribute would be innerText
that contains only text without tags. I really need the HTML. Is there a painless way to achieve this?
回答1:
You could use another XPath expression instead (of selecting the parent node and innerHTML), ./../node()
which selects the parent element.
Using node()
is important so you get both elements and text nodes.
If you know theres only one <img/>
element, you can also use //*[img]/node()
.
来源:https://stackoverflow.com/questions/18128737/get-inner-html-of-parent-element-with-php-and-xpath