As we know, to get node html with its inner tags we have to use
$node->ownerDocument->saveXML($node);
instead of
$node->nodeValue;
because the last method strips all the inner tags
But what if I get a node like
$dom->getElementsByTagName('tr')[2]->getElementsByTagName('td')[5]
How can I get its html with its inner tags without writing lines like
$dom->getElementsByTagName('tr')[2]->getElementsByTagName('td')[5]->ownerDocument->saveXML($dom->getElementsByTagName('tr')[2]->getElementsByTagName('td')[5])
Is there another shorter way?
You didn't well understand what DOMNode::ownerDocument
is. This property returns nothing more than the DOMDocument
instance to which the DOMNode
instance belongs ( $dom
in your code).
So you can replace your long line with:
$cell = $dom->getElementsByTagName('tr')[2]->getElementsByTagName('td')[5];
echo $dom->saveXML($cell);
Note that you can also shorten your code using DOMXPath.
$xp = new DOMXPath($dom);
echo $dom->saveXML($xp->query('//tr[3]/td[6]')->item(0));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.