6

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

Ben G
  • 26,091
  • 34
  • 103
  • 170

2 Answers2

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

CodePad.

alex
  • 479,566
  • 201
  • 878
  • 984
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
  • 155,785
  • 88
  • 678
  • 743