【发布时间】:2016-03-01 05:36:43
【问题描述】:
我创建了一个正则表达式来匹配输入中的域名:
$pattern = '/\w+\..{2,3}(?:\..{2,3})?(?:$|(?=\/))/i';
$url = 'http://wwe.com';
if (preg_match($pattern, $url, $matches) === 1) {
echo $matches[0];
}
这个输入很好用:
http://google.com // output:google.com
但我无法为这些输入实现它:(如果用户输入额外的www
http://www.google.com // output:google.com
http://www.www.google.com // output:google.com
我错过了什么?
对此的任何帮助将不胜感激
【问题讨论】:
-
为什么不使用
parse_url()? -
@drew010 我不想返回数组
-
只需返回 $url['host'] 并让它为您解析。