Helo,
I am a regular expression noob, trying to find resources online but the language is so foreign to me. Appreciate if anyone can help to shed some light on this.
Basically, I have successfully able to use dom method to find an image url in "img" tag using getAttribute('src') in a random string.
Example:
$string = '<a><img src=http://123.com/123.jpg/></a><img src=http://test.com/test.jpg/>'; //some random string
$url = get_img_url($string) //function of getting a url from a string.
Now here comes the challenge. How do I replace $url with another url, say, $url2? And subsequently save to $newstring? I have thought of using preg_replace, but how do I go about having this?
$url2 = 'http://example.com/content_image/blogger_1.jpg';
$find = ??? //find $url i gotten in the function above in $string
$replace = ??? //replace with $url2
$newstring = preg_replace($find, $replace, $string);
Any help would be appreciated! Thanks guys!
Edited: Appended desired output, thanks Amit Joki!
echo $newstring;
Result would be:
<a><img src=http://example.com/content_image/blogger_1.jpg/></a><img src=http://test.com/test.jpg/>