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?
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();
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