0

我正在处理评论问题,我希望我的用户能够发布图片网址,我将其更改为带有标签的图片网址

我的功能

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;

我的功能完成了一半,但我不确定如何解决已经有 html 标签的图像

这就是我得到的

在此处输入图像描述

源代码:

The text you want to filter goes here. <img src="&lt;br/&gt;&lt;img src=http://static.php.net/www.php.net/images/php.gif&gt;"> http://www.facebook.com <br><img src="http://static.php.net/www.php.net/images/php.gif">
4

1 回答 1

0

您可以先解码 html 标签 ( htmlspecialchars_decode ),然后剥离标签 ( strip_tags ) 并将 'src=' 替换为空白

于 2013-09-27T05:38:03.457 回答