I'm importing some arbitrary HTML into a DOMDocument
using the loadHTML()
function, eg.:
$html = '<p><a href="test.php">Test</a></p>';
$doc = new DOMDocument;
$doc->loadHTML($html);
I then want to change a few attributes/node values using DOMDocument
methods which I can do no problem.
Once I've made these changes I'd like to export the HTML string (using ->saveHTML()
), without the <html><body>...
tags that the DOMDocument
automatically adds to the HTML.
I understand why these are added (to ensure a valid document), but how would I go about just getting my edited HTML back (essentially everything between the <body>
tags)?
I have read this post and while it offers some solutions I would rather do this 'properly', i.e. without using a string replace on the <body>
tags. Validity of the HTML is not an issue as it's run through an HTML purifier before hand.
Any ideas? Thanks.
EDIT
I'm aware of the $node
parameter added to saveHTML()
in PHP 5.3.6, unfortunately I'm stuck with 5.2.