我这里有这个代码:
// get host name from URL
preg_match('@^(?:http://)?([^/]+)@i',
"http://www.joomla.subdomain.php.net/index.html", $matches);
$host = $matches[1];
// get last two segments of host name
preg_match('/[^.]+\.[^.]+$/', $host, $matches);
echo "domain name is: {$matches[0]}\n";
输出将为
php.net
我只需要
php
没有 .net
最佳答案
虽然这里的正则表达式很好,但我推荐 parse_url
$host = parse_url('http://www.joomla.subdomain.php.net/index.html', PHP_URL_HOST);
$domains = explode('.', $host);
echo $domains[count($domains)-2];
这适用于 .com、.org、.net 等 TLD,但不适用于 .co.uk 或 .com.mx。您需要更多的逻辑(很可能是一组 tld)来解析它们。
关于php - 获取没有 TLD 的域名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7760797/