Home >Backend Development >PHP Tutorial >How Can I Retrieve Inner and Outer HTML from a DOM Node in PHP?
DOM Manipulation in PHP: Retrieving Inner and Outer HTML
In PHP's DOM implementation, retrieving the inner or outer HTML of a DOM node is not straightforward. Unlike other development environments, PHP lacks native functions specifically designed for this purpose.
Solution
To overcome this, developers have devised reliable methods using DOM traversal and string manipulation. Here's a solution that accurately extracts both inner and outer HTML:
function DOMinnerHTML(DOMNode $element) { $innerHTML = ""; $children = $element->childNodes; foreach ($children as $child) { $innerHTML .= $element->ownerDocument->saveHTML($child); } return $innerHTML; } // Usage for outerHTML: // Recursively call DOMinnerHTML with parent as a parameter
Example:
$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); }
Note:
This solution leverages the saveHTML() method for node traversal and HTML extraction. It iterates through the child nodes of the specified element, recursively processing them to build the resulting inner or outer HTML.
The above is the detailed content of How Can I Retrieve Inner and Outer HTML from a DOM Node in PHP?. For more information, please follow other related articles on the PHP Chinese website!