After I rename an image I would like to perform a search on all HTML Pages within a directory. The idea is to find all pages with the old name and update the page with new name.
I am having a problem with the string replacement portion.
$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);
}
}
Examples of what I am searching for and replacing.
Find src="/images/old.jpg" replace src="/images/new.jpg"
Find src="http://www.domain.com/images/old.jpg replace src="http://www.domain.com/images/new.jpg"
Find src="/old.jpg ignore
Find src="http://www.anyotherdomain/old.jpg ignore