Delete subdomain from url string if subdomain is found

php, url-rewriting

Solution

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];
}

Problem

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']; ``` But this returns this only: ``` domain.com second.com third.com fourth.fifth.com sixth.com seventh.eigth.com ``` I would need this output though: ``` domain.com second.com third.com fifth.com sixth.com eigth.com ```

Original source