preg_match表达式如何忽略一个字符

1 php regex preg-match

我绝对是一个新手,并没有冒险到这个级别,但需要能够将一个域剥离到只有搜索功能的主机名.我查看并发现下面的内容几乎可以使用,除非域名中有任何-内容.因此,http://www.example.com剥离到现在example.com,www.example.comwww.exa-mple.com变成example.com.

$pattern = '/\w+\..{2,3}(?:\..{2,3})?(?:$|(?=\/))/i';
$url = $myurl;
if (preg_match($pattern, $url, $matches) === 1) {

    $mydom = $matches[0];
}
Run Code Online (Sandbox Code Playgroud)

在表达式中必须更改哪些内容才能接受-域名?

Sil*_*ost 5

你的功能会更好parse_url:

parse_url($url)
Run Code Online (Sandbox Code Playgroud)

http://如果网址没有以它开头,则只是前置.