I am importing some arbitrary HTML into a DOMDocument using the loadHTML() function, for example:
$html = '<p><a href="test.php">Test</a></p>'; $doc = new DOMDocument; $doc->loadHTML($html);
Then I want to change some attribute / node values using the DOMDocument methods, which I can do without any problems.
Once I made these changes, I would like to export the HTML string (using ->saveHTML() ), without the <html><body>... DOMDocument that the DOMDocument automatically adds to the HTML.
I understand why they were added (to provide a valid document), but how would I just want to return my edited HTML (essentially everything between the <body> tags)?
I read this post , and while it offers some solutions, I would prefer to do it “correctly”, that is, without using string replacement in the <body> tags. HTML validity is not a problem as it goes through an HTML cleaner before starting work.
Any ideas? Thanks.
EDIT
I am aware of the $node parameter added to saveHTML() in PHP 5.3.6, unfortunately I am stuck with 5.2.
html php domdocument
Clive
source share