Consider this php code:
$content = '<img />'."
".'<img />';
$doc = new DomDocument();
$doc->loadHTML($content);
echo $doc->saveHTML();
The output (including the wrapping HTML/body etc) gives me:
<img><img>
with no space between the images.
Calling:
$doc->preserveWhiteSpace = true;
doesn't change anything.
How do I preserve the white space in the original HTML?
Answering my own question - it's buggy behaviour of an old version of libxml2:
https://bugs.php.net/bug.php?id=50278
This issue is solved by passing LIBXML_HTML_NODEFDTD as option when loading the document. This constant is available as of PHP 5.4.0 when libxml2 >= 2.7.8 is used. See http://3v4l.org/qs4TC.
The shared server I'm on uses 2.7.6 so not sure it helps me, but I can see if they can upgrade. Hope this helps someone else.
White spaces are collapsed in HTML.
You can learn about how to add white spaces using tutorials such as this one:
http://www.wikihow.com/Insert-Spaces-in-HTML
but in your case, instead of using "" use "
<br><br>
" and you will get your "line break" characters.
All HTML Entities (that are explained in that tutorial linked above) can be found in this reference page:
https://dev.w3.org/html5/html-author/charref
Please note: You should really be using CSS for the visual aspects on your page so if you are just wanting spacing between your images then use things like margins rather than HTML tags.
Have you tried using nl2br
?
$doc->loadHTML(nl2br($content));
nl2br — Inserts HTML line breaks before all newlines in a string