I need to take any img tag within a string, and add an a tag around it.
E.g.
$content= "Click for more info <img src="\http://www.domain.com/1.jpg\"" />";
Would need to be replaced with
"Click for more info <a href=\"http://www.domain.com/1.jpg\"<img src="\http://www.domain.com/1.jpg\"" /></a>";
My current script is:
$content = $row_rsGetStudy['content'];
$doc = new DOMDocument();
$doc->loadHTML($content);
$imageTags = $doc->getElementsByTagName('img');
foreach($imageTags as $tag) {
$content = preg_replace("/<img[^>]+\>/i", "<a href=\"$tag\"><img src=\"$tag\" /></a>", $content);
}
echo $content
This gives me the following error: Catchable fatal error: Object of class DOMElement could not be converted to string
Any ideas on where I'm going wrong?