Home >Backend Development >PHP Tutorial >How to Retrieve the Outer HTML of DOM Elements in PHP?
Retrieve Outer HTML of DOM Elements in PHP
You've encountered an issue while attempting to obtain the outer HTML of hyperlink elements within a DOM structure using the outerHTML property. The output only reflects the inner HTML, excluding the opening and closing tags.
Solution:
In PHP versions 5.3.6 and later, you can utilize the saveHtml method, which accepts a DOM node as an argument:
$domDocument->saveHtml($nodeToGetTheOuterHtmlFrom);
Alternative for Earlier PHP Versions:
For PHP versions prior to 5.3.6, saveXml can be used. While it generates XML-compliant markup, it may not create discrepancies for anchor elements:
$domDocument->saveXml($nodeToGetTheOuterHtmlFrom);
Refer to the following URL for further details:
http://blog.gordon-oheim.biz/2011-03-17-The-DOM-Goodie-in-PHP-5.3.6/
The above is the detailed content of How to Retrieve the Outer HTML of DOM Elements in PHP?. For more information, please follow other related articles on the PHP Chinese website!