Extract the TLD from a domain name

There is no standard PHP function to extract the TLD from a domain name, parsing an URL with parse_url() will return the host name and other URL information. If you need only the TLD, for example to query the WHOIS data of a given domain name, the following script should be useful. To run this script you need a database with all TLD’s. The snippet is using a database table which is required for the scripts functionality, download the MySQL table dump here.

The current version isn’t updated yet for new TLD’s like .site or .club. If you like to provide an update, please contact me.


View this gist on GitHub