如何返回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();
}
}
不幸的是,当我尝试获取像 < 这样的完整超链接的 html 代码时,
outerHTML
不起作用。 a href='http://www.myurl.com/video.flv'>
有什么想法如何实现这一点吗?
I'm trying to replace video links inside a string - here's my code:
$doc = new DOMDocument();
$doc->loadHTML($content);
foreach ($doc->getElementsByTagName("a") as $link)
{
$url = $link->getAttribute("href");
if(strpos($url, ".flv"))
{
echo $link->outerHTML();
}
}
Unfortunately, outerHTML
doesn't work when I'm trying to get the html code for the full hyperlink like <a href='http://www.myurl.com/video.flv'></a>
Any ideas how to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从 PHP 5.3.6 开始,您可以将节点传递给
saveHtml
,例如,以前版本的 PHP 没有实现这种可能性。您必须使用
saveXml()
,但这会创建符合 XML 的标记。对于元素,这应该不是问题。
请参阅 http://blog .gordon-oheim.biz/2011-03-17-DOM-Goodie-in-PHP-5.3.6/
As of PHP 5.3.6 you can pass a node to
saveHtml
, e.g.Previous versions of PHP did not implement that possibility. You'd have to use
saveXml()
, but that would create XML compliant markup. In the case of an<a>
element, that shouldn't be an issue though.See http://blog.gordon-oheim.biz/2011-03-17-The-DOM-Goodie-in-PHP-5.3.6/
您可以在 DOM 部分<的用户注释中找到一些建议PHP 手册的 /strong>。
例如,这是 xwisdom 发布的一篇文章:
You can find a couple of propositions in the users notes of the DOM section of the PHP Manual.
For example, here's one posted by xwisdom :
最好的解决方案是定义您自己的函数,它将返回您的outerhtml:
而不是您可以在代码中使用的
The best possible solution is to define your own function which will return you outerhtml:
than you can use in your code
将包含 href 的文件重命名为 links.html 或 links.html,以表示其中包含 flv 的 google.com/fly.html,或者将 flv 更改为 wmv 等您想要的 href(如果还有其他 href)
它也会把它们捡起来
Rename a file with href to links.html or links.html to say google.com/fly.html that has flv in it or change flv to wmv etc you want href from if there are other href
it will pick them up as well