【发布时间】:2011-11-20 19:54:02
【问题描述】:
重命名图像后,我想在目录中的所有 HTML 页面上执行搜索。这个想法是找到所有具有旧名称的页面并使用新名称更新页面。
字符串替换部分有问题。
$files = glob('/directory/*.html');
foreach($files AS $file) {
$html = file_get_contents($file);
$find = "old.jpg";
preg_match("#src=(\"?|'?)(http://www.domain.com/images/|/images/)?$find(\"?|'?)#si", $html, $image);
if (!empty($image[1])) {
//find and replace all occurrences within page I need help with.
$write = fopen($file, 'w');
fwrite($write, $new_html);
fclose($write);
}
}
我正在搜索和替换的示例。
查找 src="/images/old.jpg" 替换 src="/images/new.jpg"
找到 src="http://www.domain.com/images/old.jpg 替换 src="http://www.domain.com/images/new.jpg"
查找 src="/old.jpg 忽略
查找 src="http://www.anyotherdomain/old.jpg 忽略
【问题讨论】:
-
哎哟!您不能只在页面中动态地回显 URL?
-
为什么不让 PHP 页面将图像的 URL 作为变量包含在内?
-
这些是静态 HTML 页面而不是 PHP。当我客户通过重命名更改图像时,我也想更改图像引用。
标签: php preg-match str-replace