Re: [PHP] reg exp matching/replacing

2002-10-03 Thread Jason D

Jen,

Don't use eregi_replace when str_replace will do.

$contents = str_replace($matches[0][$i], $blah, $contents);

Also the dots should be escaped or gif and jpg without 
dots before them will be matched.

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



Tired of all the SPAM in your inbox? Switch to LYCOS MAIL PLUS
http://www.mail.lycos.com/brandPage.shtml?pageId=plus

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




[PHP] reg exp matching/replacing

2002-10-02 Thread Jennifer Swofford

I'm getting much closer to achieving my goal here.  Now, I'm missing yet one more 
element.  Here is my code:

--
$filename = "newexample.html";

$fd = fopen ($filename, "r");

$contents = fread ($fd, filesize ($filename));

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

for ($i=0; $i< count($matches[0]); $i++) {

echo "matched: ".$matches[0][$i]."\n";

$blah = basename($matches[0][$i]);

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

print $contents;

fclose ($fd);
--

In this PHP script I am reading in the contents from "newexample.html", going through 
it and looking for image strings ("something/.../imagename.gif|jpg"), and stripping 
them down to their basenames ("imagename.gif|jpg").  What I must ALSO do is print the 
contents of the html file with the new, stripped down image strings.  What's going on 
in the above code is that each time you get to:

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

it goes through the entire html file and replaces every image string with the same 
replacement, so when the script is done executing, every image string in the html file 
is identical.  Of course, what I *want* to do is replace them all individually.  So if 
the html file has:





I want those strings to be replaced with:





But, the code as I have it is just replacing them all with  
since it was the last one to have matched.

How might I go through and replace them one by one?  Obscure hints are welcome.  :)

Thanks,
Jen