Extract the innerHTML of a DOMNode in PHP5
Posted by admin | Filed under PHP
Is there an easier way to extract the innerHTML from a DOMNode in PHP5?
/**
* Extracts the innerHTML of a DOMNode.
*
* The function makes a new DOMDocument, imports the DOMNode,
* appends it to the new DOMDocument and returns the HTML of the DOMDocument
*
* @param DOMNode $node DOMNode we want to extract the innerHTML from
* @return string innerHTML of the node
* @author Sergi de Pablos
*/
function getInnerHTML(DOMNode $node)
{
try {
// We create a new
$document = new DOMDocument();
$newNode = $document->importNode($node,true);
$document->appendChild($newNode);
return $document->saveHTML();
} catch (DOMException $e) {
echo "\n" . $e->getMessage() . "\n";
}
}
Tags: DOMDocument, DOMNode, innerHTML, PHP