php – How to get innerHTML of DOMNode?-ThrowExceptions

Exception or error:

What function do you use to get innerHTML of a given DOMNode in the PHP DOM implementation? Can someone give reliable solution?

Of course outerHTML will do too.

How to solve:

Compare this updated variant with PHP Manual User Note #89718:

function DOMinnerHTML(DOMNode $element) 
    $innerHTML = ""; 
    $children  = $element->childNodes;

    foreach ($children as $child) 
        $innerHTML .= $element->ownerDocument->saveHTML($child);

    return $innerHTML; 


$dom= new DOMDocument(); 
$dom->preserveWhiteSpace = false;
$dom->formatOutput       = true;

$domTables = $dom->getElementsByTagName("table"); 

// Iterate over DOMNodeList (Implements Traversable)
foreach ($domTables as $table) 
    echo DOMinnerHTML($table); 


Here is a version in a functional programming style:

function innerHTML($node) {
    return implode(array_map([$node->ownerDocument,"saveHTML"], 


To return the html of an element, you can use C14N():

$dom = new DOMDocument();
$x = new DOMXpath($dom);
foreach($x->query('//table') as $table){
    echo $table->C14N();


A simplified version of Haim Evgi’s answer:


function innerHTML(\DOMElement $element)
    $doc = $element->ownerDocument;

    $html = '';

    foreach ($element->childNodes as $node) {
        $html .= $doc->saveHTML($node);

    return $html;

Example usage:


$doc = new \DOMDocument();
$doc->loadHTML("<body><div id='foo'><p>This is <b>an <i>example</i></b> paragraph<br>\n\ncontaining newlines.</p><p>This is another paragraph.</p></div></body>");

print innerHTML($doc->getElementById('foo'));

<p>This is <b>an <i>example</i></b> paragraph<br>

containing newlines.</p>
<p>This is another paragraph.</p>

There’s no need to set preserveWhiteSpace or formatOutput.


In addition to trincot’s nice version with array_map and implode but this time with array_reduce:

return array_reduce(
   function ($carry, \DOMNode $child) {
        return $carry.$child->ownerDocument->saveHTML($child);

Still don’t understand, why there’s no reduce() method which accepts arrays and iterators alike.


function setnodevalue($doc, $node, $newvalue){
  while($node->childNodes->length> 0){
  $fragment= $doc->createDocumentFragment();
  $fragment->preserveWhiteSpace= false;
    $nod= $doc->importNode($fragment, true);


Here’s another approach based on this comment by Drupella on, that worked well for my project. It defines the innerHTML() by creating a new DOMDocument, importing and appending to it the target node, instead of explicitly iterating over child nodes.


Let’s define this helper function:

function innerHTML( \DOMNode $n, $include_target_tag = true ) {
  $doc = new \DOMDocument();
  $doc->appendChild( $doc->importNode( $n, true ) );
  $html = trim( $doc->saveHTML() );
  if ( $include_target_tag ) {
      return $html;
  return preg_replace( '@^<' . $n->nodeName .'[^>]*>|</'. $n->nodeName .'>$@', '', $html );

where we can include/exclude the outer target tag through the second input argument.

Usage Example

Here we extract the inner HTML for a target tag given by the “first” id attribute:

$html = '<div id="first"><h1>Hello</h1></div><div id="second"><p>World!</p></div>';
$doc  = new \DOMDocument();
$doc->loadHTML( $html );
$node = $doc->getElementById( 'first' );

if ( $node instanceof \DOMNode ) {

    echo innerHTML( $node, true );
    // Output: <div id="first"><h1>Hello</h1></div>    

    echo innerHTML( $node, false );
    // Output: <h1>Hello</h1>

Live example:


Old query, but there is a built-in method to do that. Just pass the target node to DomDocument->saveHtml().

Full example:

$html = '<div><p>ciao questa è una <b>prova</b>.</p></div>';
$dom = new DomDocument($html);
$xpath = new DOMXPath($dom);
$node = $xpath->query('.//div/*'); // with * you get inner html without surrounding div tag; without * you get inner html with surrounding div tag
$innerHtml = $dom->saveHtml($node);

Output: <p>ciao questa è una <b>prova</b>.</p>

Leave a Reply

Your email address will not be published. Required fields are marked *