i want to convert a string containing image links to regular hyperlinks.
$string = "blah blah blah, an image! <img src="http://blah.com/image.jpg" alt="the image"> more blah";
//output:
blah blah blah, an image! <a href="http://blah.com/image.jpg">the image</a> more blah
basically, it takes the src link and makes it a regular hyperlink, and takes the value of alt and makes that into the title of the hyperlink.
been reading through some examples and string expression pages but i'm really confused on constructing such an expression (or series of expressions) to actually accomplish this task.
i wrote something that would be able to convert just a single image link in a string to what i wanted (probably really inefficient too, i'm still learning), but this really isn't applicable when given surrounding text.
$text3="<img src=\"http://blah.com/image.jpg\" alt=\"the title\">";
if (eregi("(<img src=\"http[^>]*>)", $text3))
{
if (eregi("alt", $text3))
{
$alt = strstr($text3, "alt=\"");
$alt = str_replace("alt=\"","",$alt);
$alt = str_replace("\">","",$alt);
}
else
$alt = "image";
$imgarray = explode(" ", $text3);
$url = str_replace("src=\"","",$imgarray[1]);
$url = str_replace("\"","",$url);
$link = "<a href=\"".$url."\">".$alt."</a>";
echo $link;
}
any advice or related example code would be greatly appreciated...
i have a damn good feeling it involves using preg_replace but i'm still clueless..