如果您想要一个快速简单的解决方案,无需外部调用并根据预定义的数组进行检查。也适用于“www.domain.gallery”等新域名,这与最受欢迎的答案不同。
function get_domain($host){
$myhost = strtolower(trim($host));
$count = substr_count($myhost, '.');
if($count === 2){
if(strlen(explode('.', $myhost)[1]) > 3) $myhost = explode('.', $myhost, 2)[1];
} else if($count > 2){
$myhost = get_domain(explode('.', $myhost, 2)[1]);
}
return $myhost;
}
- domain.com-> domain.com
- sub.domain.com-> domain.com
- www.domain.com-> domain.com
- www.sub.sub.domain.com-> domain.com
- domain.co.uk -> domain.co.uk
- sub.domain.co.uk-> domain.co.uk
- www.domain.co.uk-> domain.co.uk
- www.sub.sub.domain.co.uk-> domain.co.uk
- domain.photography -> domain.photography
- www.domain.photography -> domain.photography
- www.sub.domain.photography -> domain.photography