2013-02-05 63 views
0

我使用Regexpal.com,我看到這個SO Question怎樣的preg_replace所有鏈接到鏡像文件(*和*沒有HTTPS?)

我想不匹配的字符串。

我需要用字符串 <<IMAGE:{link}>> preg_replace所有鏈接到圖像的鏈接。 所以我想用(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

@阿爾瓦羅·維卡里奧G.用的OR'ing *無*(HTTPS?) – Ted

+0

你是什麼意思與 「試圖不匹配字符串」?除此之外,已有[關於此主題的許多問題](https://www.google.com/search?q=stack+overflow+php+find+urls+in+text)。 –

+0

這是一段文字,而不是需要解析的鏈接 – Ted

回答

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

+0

這看起來不錯,但它也與dsfhttps://facebook.com/images/2323.jpg相匹配,這不是有效的鏈接! – Ted

相關問題