如果找到子域,则从 url 字符串中删除子域

2022-01-04 00:00:00 php url-rewriting

我有一个这样的域数组:

I have an array of domains like this:

domain.com
second.com
www.third.com
www.fourth.fifth.com
sixth.com
seventh.eigth.com

我想要的是一个只返回主机的函数.没有子域.

what I want is a function to return me the host only. Without subdomain.

这段代码是我到目前为止获取主机名的代码:

This code is what i have so far for getting the hostname:

$parse = parse_url($url);
$domain = $parse['host'];

但这仅返回:

domain.com
second.com
third.com
fourth.fifth.com
sixth.com
seventh.eigth.com

不过我需要这个输出:

domain.com
second.com
third.com
fifth.com
sixth.com
eigth.com

推荐答案

function giveHost($host_with_subdomain) {
    $array = explode(".", $host_with_subdomain);

    return (array_key_exists(count($array) - 2, $array) ? $array[count($array) - 2] : "").".".$array[count($array) - 1];
}

相关文章