Welcome to WebmasterWorld Guest from 22.214.171.124
I'm trying to replace all images that are found within a string with a different image location. I can find the images and store those in an array, but not sure how to replace those with a modifed value. Here's what I have so far:
preg_match_all("¦src\=\"?'?`?([[:alnum:]:?=&@/._+-]+)\"?'?`?¦i", $string, $matches);
This puts the strings that I need to replace into $matches.
I need to take those strings, modify them, and then replace the original string with the new image links. Any ideas?
Coppied from php.net:
The behavior of this function is almost identical to preg_replace(), except for the fact that instead of replacement parameter, one should specify a callback that will be called and passed an array of matched elements in the subject string. The callback should return the replacement string.
// this text was used in 2002
// we want to get this up to date for 2003
$text = "April fools day is 04/01/2002\n";
$text.= "Last christmas was 12/24/2001\n";
// the callback function
// as usual: $matches is the complete match
// $matches the match for the first subpattern
// enclosed in '(...)' and so on
// result is:
// April fools day is 04/01/2003
// Last christmas was 12/24/2002
If you dont need to use a callback you can just use the regular search and replace:
mixed preg_replace ( mixed pattern, mixed replacement, mixed subject [, int limit [, int &count]] )
Now I'm trying to match just the image name from the first regex match which is the entire src of the image. I'm trying to use the following to match against http://www.example.com/images/asdf.jpg
preg_match("/\/.+?\.jpg/", $matches, $imagematch);
I'm trying to just return asdf.jpg