可能重复:
Regex to match URL
我知道regexp应该可以做到这一点,但我无法弄清楚如何做到这一点。我不能搜索一串文字并仅返回网址。你会怎么用PHP做这个?
hello there everyone visit this link: http://google.com and this one as well http://amazon.com
将返回:
有什么想法吗?
答案 0 :(得分: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]);