php domdocument creates self closing tag that breaks html

domdocument, php

Solution

You can use the option `LIBXML_NOEMPTYTAG`:

$doc->saveXML($node, LIBXML_NOEMPTYTAG);

This will force expanding `<iframe />` to `<iframe></iframe>`

However, you can also use `saveHTML()` (which is preferred). This will preserve HTML tags properly:

$doc->saveHTML();

Problem

I'm parsing user input text & converting youtube urls into an iframe using a regex so I end up with the following: ``` <iframe title="YouTube video player" width="640" height="370" src="http://www.youtube.com/embed/*id*" frameborder="0" allowfullscreen></iframe> ``` The text is then parsed through `DOMdocument()` which converts the above into a self closing tag: ``` <iframe class="EmbeddedVideo" title="YouTube video player" width="640" height="370" src="http://www.youtube.com/embed/xP4HhaUMB3I" frameborder="0" allowfullscreen=""/> ``` Which breaks the page in Chrome & Opera. If I manually change the html back to: ``` <iframe class="EmbeddedVideo" title="YouTube video player" width="640" height="370" src="http://www.youtube.com/embed/xP4HhaUMB3I" frameborder="0" allowfullscreen=""></iframe> ``` it works. Firstly, is there anything wrong with the self closing tag (because I can't see anything)? Is there a way to force domdocument() to use a 'proper' closing tag?

Original source

Related problems