preg_match模式匹配表达式在php中获取URL

时间:2013-08-02 08:08:59

标签: php regex url pattern-matching preg-match

我的文字如下:
我有这个文字'文字(在括号中)结束' 在这里:www.url1.com
在这里:一些文字(www.url2.com)

我想提取 www.url2.com 并存储在变量中 我正在使用模式:preg_match('/(?< =()(。+)(?=))/是',$ message,$ match);
这导致第1行中括号的原因。 提取的字符串是:在括号中)
请通过提供合适的正则表达式帮助我。

2 个答案:

答案 0 :(得分:0)

if (preg_match('/\(\s?(www\.\w+\.com)\s?\)/', $text, $matches)){
    $var = $matches[1];
}

假定为.com。如果您想允许.net.org,则可以将其修改为:

if (preg_match('/\(\s?(www\.\w+\.(com|net|org))\s?\)/', $text, $matches)){
    $var = $matches[1];
}

答案 1 :(得分:0)

preg_match("#\((www\.[^\s]+)\)#", $text, $match);

echo $match[1];