How to get Domain Name From Subdomain in PHP

I need to write a function to parse variables which contain domain names. It’s best I explain this with an example, the variable could contain any of these things:

If you want a fast simple solution, without external calls and checking against predefined arrays. Works for new domains like “www.example.xyz” also, unlike the most popular answer.

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;
}
  • example.xyz -> example.xyz
  • sub.example.xyz -> example.xyz
  • www.example.xyz -> example.xyz
  • www.sub.sub.example.xyz -> example.xyz
  • example.co.uk -> example.co.uk
  • sub.example.co.uk -> example.co.uk
  • www.example.co.uk -> example.co.uk
  • www.sub.sub.example.co.uk -> example.co.uk
  • example.photography -> example.photography
  • www.example.photography -> example.photography
  • www.sub.example.photography -> example.photography

Best Practices Implementation

fetch_mozilla_tlds.php contains caching algorhythm. This line is important:

$mozillaTlds = file('http://mxr.mozilla.org/mozilla-central/source/netwerk/dns/effective_tld_names.dat?raw=1');

The main file used inside the application is this:

function isTopLevelDomain($domain)
{
    $domainParts = explode('.', $domain);
    if (count($domainParts) == 1) {
        return false;
    }

    $previousDomainParts = $domainParts;
    array_shift($previousDomainParts);

    $tld = implode('.', $previousDomainParts);

    return isDomainExtension($tld);
}

function isDomainExtension($domain)
{
    $tlds = getTLDs();

    /**
     * direct hit
     */
    if (in_array($domain, $tlds)) {
        return true;
    }

    if (in_array('!'. $domain, $tlds)) {
        return false;
    }

    $domainParts = explode('.', $domain);

    if (count($domainParts) == 1) {
        return false;
    }

    $previousDomainParts = $domainParts;

    array_shift($previousDomainParts);
    array_unshift($previousDomainParts, '*');

    $wildcardDomain = implode('.', $previousDomainParts);

    return in_array($wildcardDomain, $tlds);
}

function getTLDs()
{
    static $mozillaTlds = array();

    if (empty($mozillaTlds)) {
        require 'fetch_mozilla_tlds.php';
        /* @var $mozillaTlds array */
    }

    return $mozillaTlds;
}