【发布时间】:2017-06-18 02:35:55
【问题描述】:
我已经获取了一个 HTML 页面,我如何用这些绝对 URL 替换相对 img src?
在我的内容 html 中:
<img width="23" height="23" class="img" src="/static/img/facebook.png" alt="Facebook">
<img width="23" height="23" class="img" src="/static/img/tw.png" alt="tw">
(路径不稳定)
<img width="130" =="" height="200" alt="" src="http://otherdomain/files/ads/00905819.gif">
在 html 内容中,我有其他具有绝对 src 的图像 src,我只想更改相对 src 图像。 例如:
<img width="23" height="23" class="img" src="/static/img/facebook.png" alt="Facebook">
<img width="23" height="23" class="img" src="/images/xx.png" alt="xxx">
到
<img width="23" height="23" class="img" src="http://example.com/static/img/facebook.png" alt="Facebook">
我的 preg_replace:
$html = preg_replace("(]*src\s*=\s*[\"'])(?!http)([^\"'>]+)([\"'>]+) ", '$1http://www.example.com$2$3', $x1);
但这会替换所有图片src
【问题讨论】: