Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php domdocument creates self closing tag that breaks html [duplicate]

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?

like image 660
Orinoco Avatar asked Jan 11 '14 15:01

Orinoco


1 Answers

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();
like image 160
hek2mgl Avatar answered Nov 09 '22 16:11

hek2mgl