Hello all,

I am trying to use php to read through an html file and replace all image
paths, like such:

Change "images/firstimage.gif" to "http://www.blah.com/firstimage.gif"; ...
Change "somesuch/images/secondimage.jpg" to

So I am going through the file and matching the image path strings,
stripping them down to just the image name using "basename", and then adding
"http://www.blah.com/"; onto the beginning of them.  Except it's not working,
and I'm not sure where my logic has gone awry.  Instead of replacing each
image path with its respective image name, it is replacing all image paths
with the *same* image name.

Here is the code.  (Yes I'm new, I apologize for the mess, there are a few
lines in there I'm using for debugging purposes, to show me what it's

$filename = "newexample.html";
$fd = fopen ($filename, "r");
$contents = fread ($fd, filesize ($filename));

while (preg_match('{(")([A-z0-9_/-](\/))*[A-z0-9_/-]+(.gif|.jpg)}',
$contents, $matches)) {

echo "<b>MATCHED:</b> ".$matches[0]."<br>\n";

$base = basename($matches[0]);
$newimage = "http://www.blah.com/".$base;
echo "<br>base: ".$base."<br>";
echo "new image: ".$newimage."<br>";
echo "<hr>";

$contents = eregi_replace('\/*[A-z0-9_/-]+(.gif|.jpg)', $newimage,

print $contents;


fclose ($fd);


When I run it, I get:

MATCHED: "blah/images/first.gif

base: first.gif
new image: http://www.blah.com/first.gif
<Here it prints the contents of the html file, except all images look like
"http://www.blah.com/first.gif"; (when there are supposed to be lots of other
images, such as second.jpg, etc).>

Where have I gone wrong?  Thanks to all, as usual, for your hints and help.


PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to