繁体   English   中英

使用正则表达式查找并替换URL到HTML标签

[英]Find & replace url to HTML tag using regular expression

我希望有一个可以解析为文本的函数,然后它将所有包含(jpg | png | gif | jpeg | bmp)扩展名的链接替换为<img>标记,之后它还将替换所有其他链接。链接中没有<a>标签的(jpg | png | gif | jpeg | bmp)扩展名。

例如,它应该替换:

http://imgur.com/gallery/TpGvHBL http://i.imgur.com/TpGvHBL.jpg

<a href="http://imgur.com/gallery/TpGvHBL" target="_blank">http://imgur.com/gallery/TpGvHBL</a> <img src="http://i.imgur.com/TpGvHBL.jpg" />

================================================== =========================

目前,我可以使用以下正则表达式将图片网址替换为<img>标签:

$text = preg_replace('#((https?|ftp):\\/\\/([^\\s]*)\\.(jpg|gif|png))#', '<img src="$1" />', $text);

并在下面将普通网址替换为<a>标签:

$text = preg_replace('/(\\b(https?|ftp|file):\\/\\/[-A-Z0-9+&@#\\/%?=~_|!:,.;]*[-A-Z0-9+&@#\\/%=~_|])/i', '<a href="$1" target="_blank">$1</a>', $text);

我想要的是更改第二个正则表达式以仅替换非图像url,因为它将与我的第一个正则表达式冲突。

谢谢。

对不起,我答复很晚,我将即时答复。

所以这是我想出的解决方案:

$string = 'some test http://imgur.com/gallery/TpGvHBL http://i.imgur.com/TpGvHBL.jpg something else ...';

$result = preg_replace_callback('~\b(?:https?|ftp|file)://\S+~i', function($v){
    if(preg_match('~\.jpe?g|\.png|\.gif|\.bmp$~i', $v[0])){ // if image
        return '<img src="' . $v[0] . '">';
    }else{
        return '<a href="' . $v[0] . '" target="_blank">' . $v[0] . '</a>';
    }
}, $string);

我想到匹配所有网址,然后检查是否有图像扩展名。 当然,第一个正则表达式非常松散,您可以对其进行改进。请注意,由于我使用的是匿名函数,因此需要PHP 5.3+。

正则表达式说明:

~                   # delimiter
    \b              # word boundary
    (?:             # start of a non-capturing group
        https?      # match http or https
        |           # or
        ftp         # match ftp (you may want to add sftp o_O ?)
        |           # or
        file        # match file
    )               # end of the non-capturing group
    ://             # match ://
    \S+             # match anything except whitespace one or more times
~                   # delimiter, end of expression
i                   # set the i modifier : match case-insensitive

第二个正则表达式~\\.jpe?g|\\.png|\\.gif|\\.bmp$~i仅匹配字符串末尾的以下扩展名jpg, jpeg, png, gif and bmp

我希望这是你要找的

解决方案1:

<?php
$str="http://imgur.com/gallery/TpGvHBL http://i.imgur.com/TpGvHBL.jpg";
$new_str=explode(" ",$str);
$str="<a href=".$new_str[0]." target=_blank>".$new_str[0]."</a>";
$str.=" <img src=".$new_str[1]." />";
echo htmlentities($str);

OUTPUT:

<a href=http://imgur.com/gallery/TpGvHBL target=_blank>http://imgur.com/gallery/TpGvHBL</a> <img src=http://i.imgur.com/TpGvHBL.jpg />

解决方案2:

<?php
//$str='http://imgur.com/gallery/TpGvHBL';
$str='http://i.imgur.com/TpGvHBL.jpg';
if(is_array(getimagesize($str)))
{
echo "Image<br>";
    $str="<img src=".$str." />";
}
else
{
    echo "Link<br>";
    $str="<a href=".$str." target=_blank>".$str."</a>";
}
echo htmlentities($str);

OUTPUT:

Image
http://i.imgur.com/TpGvHBL.jpg

@hamza的RegExp缺少一些不属于URL的符号,例如引号,括号等。

我建议更改此:

~\b(?:https?|ftp|file)://\S+~i

对此:

~\b(?:https?|ftp|file):\/\/[^\s"'(){}<>|\\^~`]+~i

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM