By Dawid Ohia


2010-01-18 15:34:42 8 Comments

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.

7 comments

@birgire 2018-12-12 10:05:37

Here's another approach based on this comment by Drupella on php.net, 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.

InnerHTML

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 .'>[email protected]', '', $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:

http://sandbox.onlinephpfunctions.com/code/2714ea116aad9957c3c437d46134a1688e9133b8

@Haim Evgi 2010-01-18 15:38:51

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

<?php 
function DOMinnerHTML(DOMNode $element) 
{ 
    $innerHTML = ""; 
    $children  = $element->childNodes;

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

    return $innerHTML; 
} 
?> 

Example:

<?php 
$dom= new DOMDocument(); 
$dom->preserveWhiteSpace = false;
$dom->formatOutput       = true;
$dom->load($html_string); 

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

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

@Dawid Ohia 2010-01-18 18:59:23

Thanks. It works fine. Shouldn't $dom->preserveWhiteSpace = false; be before document load?

@hakre 2013-06-23 18:35:31

@JohnM2: Yes it should.

@hakre 2013-06-23 22:01:54

Additional notes: Since PHP 5.3.6 you can spare the temporary DOMDocument. Also one might want to replace the trim with an ltrim (or even remove it completely) to preserve a bit of the whitespace like line-breaks.

@Nate 2014-08-24 15:26:51

A function like this should be added to the DomDocument class.

@miken32 2014-10-04 22:08:13

I had to change the function declaration to expect a DOMElement instead of a DOMNode as I was passing the return from DOMDocument::getElementById(). Just in case it trips someone else up.

@Aaron Gillion 2015-06-01 06:32:27

Why do you loop through all the children? Couldn't you just saveHTML() on the $table? Look: PHP outerHTML S/O

@machineaddict 2015-12-11 11:40:31

This doesn't work. The error is "DOMDocument::saveHTML() expects exactly 0 parameters, 1 given"

@flu 2016-10-05 08:21:50

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

return array_reduce(
   iterator_to_array($node->childNodes),
   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.

@trincot 2016-08-28 16:38:26

Here is a version in a functional programming style:

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

@Alf Eaton 2016-06-28 14:42:28

A simplified version of Haim Evgi's answer:

<?php

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

    $html = '';

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

    return $html;
}

Example usage:

<?php

$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.

@Pedro Lobito 2016-05-13 19:25:36

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

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

@ajaybc 2016-05-18 04:05:54

C14N will attempt to convert the HTML to a valid XML. For example <br> will become <br></br>

@Pedro Lobito 2016-05-18 14:53:26

It's a dirty way of dump the HTML of the element, without having to use saveHTML that will output html, head and body tags.

@Chris 2014-06-05 18:55:46

function setnodevalue($doc, $node, $newvalue){
  while($node->childNodes->length> 0){
    $node->removeChild($node->firstChild);
  }
  $fragment= $doc->createDocumentFragment();
  $fragment->preserveWhiteSpace= false;
  if(!empty($newvalue)){
    $fragment->appendXML(trim($newvalue));
    $nod= $doc->importNode($fragment, true);
    $node->appendChild($nod);
  }
}

Related Questions

Sponsored Content

35 Answered Questions

[SOLVED] Reference - What does this error mean in PHP?

18 Answered Questions

[SOLVED] Reference — What does this symbol mean in PHP?

10 Answered Questions

[SOLVED] How do you use bcrypt for hashing passwords in PHP?

30 Answered Questions

[SOLVED] How to change an element's class with JavaScript?

  • 2008-10-12 20:06:43
  • Nathan Smith
  • 2483079 View
  • 2656 Score
  • 30 Answer
  • Tags:   javascript html dom

55 Answered Questions

[SOLVED] How do I check if an element is hidden in jQuery?

7 Answered Questions

[SOLVED] How does PHP 'foreach' actually work?

36 Answered Questions

[SOLVED] PHP and Enumerations

  • 2008-10-31 18:51:14
  • Henrik Paul
  • 467015 View
  • 1115 Score
  • 36 Answer
  • Tags:   php enumeration

32 Answered Questions

[SOLVED] How do I get a YouTube video thumbnail from the YouTube API?

13 Answered Questions

[SOLVED] Dude, where's my php.ini?

  • 2011-12-30 22:20:06
  • necromancer
  • 763663 View
  • 844 Score
  • 13 Answer
  • Tags:   php linux php-ini

12 Answered Questions

[SOLVED] How to replace innerHTML of a div using jQuery?

Sponsored Content