2011-08-23 58 views
0

可能重複:
Regex to match URL如何刪除一切,但URL PHP

我知道正則表達式應該能夠做到這一點,但我無法弄清楚如何做到這一點。我無法搜索一串文本並只返回網址。你將如何在PHP中做到這一點?

hello there everyone visit this link: http://google.com and this one as well http://amazon.com 

將返回:

http://google.com

http://amazon.com

任何想法?

+1

見http://stackoverflow.com/questions/1141848/regex-to-match-url –

+0

[URL正則表達式(http://www.google.com/搜索?sourceid = chrome&ie = UTF-8&q = url%20regex) – Xeoncross

回答

1

這應該工作:

$text = "hello there everyone visit this link: http://google.com and this one as well http://amazon.com"; 
preg_match_all('/\b(([\w-]+:\/\/?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|\/)))/',$text,$matches); 
print_r($matches[0]); 
+0

謝謝,很有魅力。我討厭寫正則表達式 – vincentieo