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.


@Marco Marsala 2020-03-13 15:49:35

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>

@Ivan Gusev 2020-06-16 08:40:13

Warning: DOMDocument::saveHTML() expects parameter 1 to be DOMNode, object given

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

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 .'>[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:

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

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); 

@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(
   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"], 

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

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.

@CONvid19 2016-05-13 19:25:36

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();

@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>

@CONvid19 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){
  $fragment= $doc->createDocumentFragment();
  $fragment->preserveWhiteSpace= false;
    $nod= $doc->importNode($fragment, true);

Related Questions

Sponsored Content

11 Answered Questions

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

57 Answered Questions

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

20 Answered Questions

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

37 Answered Questions

[SOLVED] PHP and Enumerations

  • 2008-10-31 18:51:14
  • Henrik Paul
  • 496106 View
  • 1157 Score
  • 37 Answer
  • Tags:   php enumeration

13 Answered Questions

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

32 Answered Questions

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

14 Answered Questions

[SOLVED] Where can I find php.ini?

  • 2011-12-30 22:20:06
  • necromancer
  • 805809 View
  • 890 Score
  • 14 Answer
  • Tags:   php linux php-ini

36 Answered Questions

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

32 Answered Questions

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

  • 2008-10-12 20:06:43
  • Nathan Smith
  • 2628519 View
  • 2818 Score
  • 32 Answer
  • Tags:   javascript html dom

7 Answered Questions

[SOLVED] How does PHP &amp;#39;foreach&amp;#39; actually work?

Sponsored Content