PHP DOM - Problem loading YouTube Embed Code

2019-07-25 04:06发布

I'm trying to do the following:

$string = "<object width=\"425\" height=\"350\">
               <param name=\"movie\" value=\"http://www.youtube.com/v/1OtdDqF5rnI&autoplay=0\"></param>
               <param name=\"wmode\" value=\"transparent\"></param>
               <embed src=\"http://www.youtube.com/v/1OtdDqF5rnI&autoplay=0\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"425\" height=\"350\"></embed>
           </object>";

$doc = new DOMDocument();
$doc->loadXML($string);

When I load the string i get the following errors:

[phpBB Debug] PHP Notice: in file /mnt/FS-2/Critical/Media/Private/Website/Online/blitz1up.com/application/controllers/articles.php on line 303: DOMDocument::loadXML() [domdocument.loadxml]: EntityRef: expecting ';' in Entity, line: 2
[phpBB Debug] PHP Notice: in file /mnt/FS-2/Critical/Media/Private/Website/Online/blitz1up.com/application/controllers/articles.php on line 303: DOMDocument::loadXML() [domdocument.loadxml]: EntityRef: expecting ';' in Entity, line: 4

I have narrowed the cause down to the YouTube URLs but am unsure of the best method to resolve this error. So any suggestions would be most welcome.

1条回答
贼婆χ
2楼-- · 2019-07-25 04:23

The URLs contain ampersands, which should be encoded as &amp; when not used for declaring entities. The parser assumes &autoload will start a new entitiy but it is never terminated with a semicolon.

For working with the HTML, consider loading the fragment with loadHTML(); and disable errors for that part:

libxml_use_internal_errors(TRUE);
$doc = new DOMDocument();
$doc->loadHTML($string);
libxml_clear_errors();
echo $doc->saveHTML();

The above would work with loadXML too.

查看更多
登录 后发表回答