Sometimes you have domains like a.really.long.url.thing.com
and you need to get the base domain name. Yes, you could run:
$domains = explode('.', $host_name);
$target = array_slice($domains, -2, 1);
$root = $target[0];
But what if it is .co.uk
? That is when you need this slightly badly written script.
It is worth noting that fr.co.uk
is a valid URL despite fr
, co
and uk
all being TLDs. So therefore, a.thing.fr.co.uk
would be converted to thing.fr.co.uk
instead of fr.co.uk
. Obviously there are bugs.