我正在使用Regexpal.com,我看到了SO Question
我试图不匹配字符串。
我需要使用字符串preg_replace所有出现的图像链接
<<IMAGE:{link}>>
。
所以我在想(https?:\/\/)?\S*(jpg|png|jpeg|bmp|gif)
OR
使用相同的内容,但 without
前面的(https?://)
这样:
Hi there this is an image link https://facebook.com/images/2323.jpg
and this one is too mysite.org/1.png
将成为:
Hi there this is an image link
<<IMAGE:https://facebook.com/images/2323.jpg>> and this one is too
<<IMAGE:mysite.org/1.png>>
答案 0 :(得分:1)
我想你想要这个...
$str = 'Hi there this is an image link https://facebook.com/images/2323.jpg
and this one is too mysite.org/1.png';
$regex = '/((https?:\/\/)?\S*(jpg|png|jpeg|bmp|gif))/';
$str = preg_replace($regex, '<<IMAGE:$1>>', $str);
echo $str;
<强>输出强>
Hi there this is an image link <<IMAGE:https://facebook.com/images/2323.jpg>>
and this one is too <<IMAGE:mysite.org/1.png>>
<强> Codepad 强>