6

What's the simplest way to get the innerHTML (tags and all) of a DOMElement using PHP's DOM functions?

Ben G
  • 25,319
  • 32
  • 97
  • 163

2 Answers2

10
$html = '';
foreach($parentElement->childNodes as $node) {
   $html .= $dom->saveHTML($node);
}

CodePad.

alex
  • 460,746
  • 196
  • 858
  • 974
3

Inner HTML

Try approach suggested by @trincot:

$html = implode(array_map([$node->ownerDocument,"saveHTML"], iterator_to_array($node->childNodes)));

Outer HTML

Try:

$html = $node->ownerDocument->saveHTML($node);

or in PHP lower than 5.3.6:

$html = $node->ownerDocument->saveXML($node);
Community
  • 1
  • 1
kenorb
  • 137,499
  • 74
  • 643
  • 694