0

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

1 回答 1

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

键盘

于 2013-02-05T12:15:18.800 回答