PHP 正则获取域名(一级域名)
<?php
// get host name from URL
preg_match('@^(?:http://)?([^/]+)@i',
"http://www.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";
?>
需要注意的是,这种方式无法正确处理一些特殊的顶级域名(例如 .co.uk、.com.cn 等)。如果需要涵盖更多顶级域名,可以考虑使用第三方库(例如 jeremykendall/php-domain-parser)来实现。
来源:www.php.net/manual/en/function.pre...
本作品采用《CC 协议》,转载必须注明作者和本文链接