使用快速可靠的方法从 link 获取域名

get domain name from link with a fast and reliable method

目前我正在使用此代码获取域名(没有 www. 或以 .com 结尾的域名):

explode('.', $url)[1];

由于此代码处于循环中,因此处理它需要很长时间。此外,它无法从 http://example.com/asd/asd.asd.html 获取 "example"。有没有另一种更快的方法来解决这个问题?

提前感谢您的回答!

最好的问候

使用parse_url()

$host = parse_url($url, PHP_URL_HOST);

PHP_URL_HOST returns 主持人

此外,使用正则表达式获取所需的主机部分:

$result = preg_match('/^(?:www\.)?([^\.]+)/', $match);