tags:

views:

161

answers:

1

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

Of course outerHTML will do too.

+2  A: 

read in :

http://php.net/manual/en/book.dom.php

<?php 
function DOMinnerHTML($element) 
{ 
    $innerHTML = ""; 
    $children = $element->childNodes; 
    foreach ($children as $child) 
    { 
        $tmp_dom = new DOMDocument(); 
        $tmp_dom->appendChild($tmp_dom->importNode($child, true)); 
        $innerHTML.=trim($tmp_dom->saveHTML()); 
    } 
    return $innerHTML; 
} 
?> 

Example:

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

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

foreach ($domTable as $tables) 
{ 
    echo DOMinnerHTML($tables); 
} 
?> 
Haim Evgi
Thanks. It works fine. Shouldn't $dom->preserveWhiteSpace = false; be before document load?
JohnM2