Get inner HTML of parent element with php and xpath

限于喜欢 提交于 2019-12-24 14:43:28

问题


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

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