【发布时间】:2010-09-27 23:55:43
【问题描述】:
可能重复:
Dynamically replace the “src” attributes of all <img> tags
有趣的故事:我不久前发布了this very question,但我没有得到我可以使用的东西,你知道,我得到的只是很多关于使用正则表达式解析 HTML 的弊端的教条。所以又来了。
我有一些 HTML,想替换所有 img 标签的“src”属性,以便它们指向另一台主机上相同图像(尽管文件名不同)的副本。
例如,给定这三个标签
<IMG SRC="../graphics/pumpkin.gif" ALT="pumpkin">
<IMG BORDER="5" SRC="redball.gif" ALT="*">
<img alt="cool image" src="http://www.crunch.com/pic.jpg"/>
我希望将它们替换为
<IMG SRC="http://myhost.com/cache/img001.gif" ALT="pumpkin">
<IMG BORDER="5" SRC="http://myhost.com/cache/img002.gif" ALT="*">
<img alt="cool image" src="http://myhost.com/cache/img003.jpg"/>
我正在尝试使用PHP Simple HTML DOM Parser,但我不明白。
include 'simple_html_dom.php';
$html = str_get_html('<html><body>
<IMG SRC="../graphics/pumpkin.gif" ALT="pumpkin">
<IMG BORDER="5" SRC="redball.gif" ALT="*">
<img alt="cool image" src="http://www.crunch.com/pic.jpg"/>
</body></html>');
接下来我该怎么做?
【问题讨论】:
-
你试过使用 DOMDocument() 吗?
-
投票关闭 Dynamically replace the "src" attributes of all <img> tags文档并不难,我发现它比“简单的 HTML DOM 解析器”更容易(主观)和更快(客观))