Reputation: 5854
I'm using the PHP DOM API to create some markup. It works OK, but a newline \n
appears after <br>
tags in certain cases. I managed to create a reprex:
$document = new DOMDocument();
$document->loadHTML('<div>text<br></div>');
$element = $document->getElementsByTagName('div')->item(0);
echo $document->saveHTML($element); // newline appears after <br>
For some reason, saveHTML()
doesn't return the same markup that was initially loaded. As I've demonstrated in the repl, adding text around the <br>
tag or using saveXML()
works correctly.
Is this some special behavior defined in the DOM spec? The problem only appears to happen when the <br>
tag is the first or last child, I.E. there's no text in between.
Upvotes: 0
Views: 464