A couple of things. eregi_replace does not have a limitation parameter,
which means that if you use it *all* the strings that match your pattern
will be replace with the new pattern. In your case, if I read your code
correctly (and I may not--I'm doing this from memory), the eregi
replacement you perform will change *all* the paths you have in your
file, and not just the first one, which, I believe, is what you're
looking for. 

Also, there shouldn't be any need to make the replacements one by one. A
single replacement should be enough if all the original paths have to be
transformed into the same URL.

For example:

ereg_replace ("{/images/}{test.(jpg|gif)}",
"\\2";, $t);

This would replace any occurrence of "/images/*.gif" and "/images/*.jpg"
to "*.gif"; or *.jpg respectively (once
again, double check my regex...doing this from memory). If you *do* need
to change each occurrence individually, then you should use preg()
instead, which uses Perl syntax and has a limitation parameter.

Also--a minor thing and I'm sure you thought of it, but you're not
saving the file at the end :-)

Hope this helps.



On Mon, 2002-10-07 at 20:01, Jennifer Swofford wrote:
> 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 ""; ...
> 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
> ""; 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
> doing.)
> <?
> $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 = "".$base;
> echo "<br>base: ".$base."<br>";
> echo "new image: ".$newimage."<br>";
> echo "<hr>";
> $contents = eregi_replace('\/*[A-z0-9_/-]+(.gif|.jpg)', $newimage,
> $contents);
> print $contents;
> }
> fclose ($fd);
> ?>
> When I run it, I get:
> ~~~~~
> MATCHED: "blah/images/first.gif
> base: first.gif
> new image:
> ------------------------------------------------------------------------
> <Here it prints the contents of the html file, except all images look like
> ""; (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.
> Jen
> -- 
> PHP General Mailing List (
> To unsubscribe, visit:

PHP General Mailing List (
To unsubscribe, visit:

Reply via email to