使用正则表达式查找并将url替换为HTML标记

问题描述 投票:1回答:3

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

例如,它应该替换:

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,因为它会与我的第一个正则表达式冲突。

谢谢。

php html regex preg-replace expression
3个回答
1
投票

对不起,迟到的回复,我马上回答。

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

$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


0
投票

我希望这就是你要找的东西

解决方案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

0
投票

@hamza's RegExp错过了一些不属于URL的符号,例如引号,括号等

我建议改变这个:

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

对此:

~\b(?:https?|ftp|file):\/\/[^\s"'(){}<>|\\^~`]+~i
© www.soinside.com 2019 - 2024. All rights reserved.