如何preg_replace所有链接到图像(使用和*没有* https?)

时间:2013-02-05 11:53:06

标签: php regex

我正在使用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>>

1 个答案:

答案 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