Finding the root domain name (and TLD, or top level domain) from a URL is challenging. Literature on the web generally leans towards a library that'll find the root domain by way of comparing the URL against all approved top level domains ... but this method introduces a degree of complexity that we wanted to avoid. The following function is one that we use to find a domain name root (or TLD). It isn't perfect, but it's close.
You may optionally return the top level domain (eg,
.net) with the argument
$tld = true.