如何返回DOMDocument的外部html?

如何返回DOMDocument的外部html?

我正在尝试替换字符串中的视频链接 - 这是我的代码:

$doc = new DOMDocument();$doc->loadHTML($content);foreach ($doc->getElementsByTagName("a") as $link) {
    $url = $link->getAttribute("href");
    if(strpos($url, ".flv"))
    {
        echo $link->outerHTML();
    }}

不幸的是,outerHTML当我试图获取完整超链接的html代码时不起作用<a href='http://www.myurl.com/video.flv'></a>

任何想法如何实现这一目标?


阿晨1998
浏览 364回答 3
3回答

烙印99

最好的解决方案是定义你自己的函数,它将返回你的outerhtml:function&nbsp;outerHTML($e)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$doc&nbsp;=&nbsp;new&nbsp;DOMDocument(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$doc->appendChild($doc->importNode($e,&nbsp;true)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$doc->saveHTML();}比你可以在你的代码中使用echo&nbsp;outerHTML($link);
打开App,查看更多内容
随时随地看视频慕课网APP