How to return outer html of DOMDocument?

As of PHP 5.3.6 you can pass a node to saveHtml, e.g.

$domDocument->saveHtml($nodeToGetTheOuterHtmlFrom);

Previous versions of PHP did not implement that possibility. You’d have to use saveXml(), but that would create XML compliant markup. In the case of an <a> element, that shouldn’t be an issue though.

See http://blog.gordon-oheim.biz/2011-03-17-The-DOM-Goodie-in-PHP-5.3.6/

Leave a Comment