Hey guys,
I have a string that is for a blog, potentially it could have an unlimited number of images in this string, what im trying to do is get the value of the src=“” and use it as the hyperlink that happens before the img
so currently they might show up
<img src="images/image1.jpg" />
<img src="images/image2.jpg" />
<img src="images/image3.jpg" />
etc
what im trying to turn this into is the following
<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>
basically trying to integrate lightbox into our blog dynamically so our user doesn’t need to manually type code she doesn’t understand.
ive tried countless things, the closest is the following, tho it gives them all the same href
preg_match_all('/<img[^>]+>/i',$qryblog[content], $imgTags);
for ($i = 0; $i < count($imgTags[0]); $i++) {
// get the source string
preg_match('/src="([^"]+)/i',$imgTags[0][$i], $imgage);
// remove opening 'src=' tag, can`t get the regex right
$origImageSrc[$id][] = str_ireplace( 'src="', '', $imgage[0]);
}
$src = $origImageSrc[$id][$count];
$yourprefix = "<a href=\\"$src\\" data-lightbox=\\"image-2\\" title=\\"Gallery\\">";
$yoursuffix = "</a>";
$content=preg_replace("#(<img[^>]*>)#s",$yourprefix."\\${1}".$yoursuffix,$qryblog[content]);
im really struggling to find a way to make this dynamically do it on the fly… does anyone have any ideas?
ive also tried using simple dom html paraser but didn’t have much luck with it
$html = str_get_html($qryblog[content]);
foreach($html->find('img') as $element)
echo $element->src . '<br>';
This happily outputs all the src’s but I cant make it do the rest