I have a string that is for a blog, potentially it could have an unlimited number of images in this string, what I am trying to do is get all of the src="" and add a prefix to the url and use it as the hyperlink.
My string:
$test = 'Hello world
<img src="images/image1.jpg" />Line 2
<img src="images/image2.jpg" />Some text
<img src="images/image3.jpg" />';
I am able to prefix href. I am able to achieve this:
<a href="images/image1.jpg"><img src="images/image1.jpg" /></a>
<a href="images/image1.jpg"><img src="images/image2.jpg" /></a>
<a href="images/image1.jpg"><img src="images/image3.jpg" /></a>
This is my code so far:
$new = preg_replace('/(<img[^>]+src="([^\\"]+)"[^>]+\\/>)/','<a href="\\2">\\1</a>',$test2);
echo $new;
I need to add foldername/as prefix in all the image src. What im trying to turn this into is the following:
<a href="images/image1.jpg"><img src="foldername/images/image1.jpg" /></a>
<a href="images/image1.jpg"><img src="foldername/images/image2.jpg" /></a>
<a href="images/image1.jpg"><img src="foldername/images/image3.jpg" /></a>
How can I do that?