I'm adding a #b hash to each link via the DOMDocument class.
$dom = new DOMDocument();
$dom->loadHTML($output);
$a_tags = $dom->getElementsByTagName('a');
foreach($a_tags as $a)
{
$value = $a->getAttribute('href');
$a->setAttribute('href', $value . '#b');
}
return $dom->saveHTML();
That works fine, however the returned output includes a DOCTYPE
declaration and a <head>
and <body>
tag. Any idea why that happens or how I can prevent that?
That's what DOMDocument::saveHTML()
generally does, yes : generate a full HTML Document, with the Doctype declaration, the <head>
tag, ...
Two possible solutions :
saveHTML()
accepts one additional parameter that might help you
str_replace()
or regex or whatever equivalent you can think of to remove the portions of HTML code you don't need.
The real problem is the way the DOM is loaded. Use this instead:
$html->loadHTML($content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
Please upvote the original answer here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With