Thursday 26 September 2013

Converting image url to complete html image tag

Converting image url to complete html image tag

I am working on comments thing and i want my user to be able to post image
url which i will change into image url with tag
my function
function ImageTag($text) {
// The Regular Expression filter
$reg_exUrl =
"/(http|https)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
// The Text you want to filter for urls
if(preg_match_all($reg_exUrl, $text, $url)) {
// make the urls hyper links
$matches = array_unique($url[0]);
foreach($matches as $match) {
$check = substr($match, -4);
if ($check == ".jpg" || $check == ".png" || $check ==
".gif" || $check == "jpeg" || $check == ".JPG" || $check
== ".PNG" || $check == ".GIF" || $check == "JPEG"){
$replacement = "<br/><img src=$match>";
$text = str_replace($match, $replacement, $text);
}
}
return nl2br($text);
} else {
// if no urls in the text just return the text
return nl2br($text);
}
}
$text = "The text you want to filter goes here. <img
src='http://static.php.net/www.php.net/images/php.gif'>
http://www.facebook.com http://static.php.net/www.php.net/images/php.gif";
$content = ImageTag($text);
echo $content;
I am half done with my function but i am not sure how to solve image which
are already have html tags
This is what i am getting

source code:
The text you want to filter goes here. <img src='<br/><img
src=http://static.php.net/www.php.net/images/php.gif>'>
http://www.facebook.com <br/><img
src=http://static.php.net/www.php.net/images/php.gif>

No comments:

Post a Comment