tldextract.php – Extract TLD, Domain And Subdomains From URLs

tldextract.php is a PHP library that accurately extracts the effective top-level domain name, registered domain and subdomains from a URL. For example, you can use it to get the domain name “google” from “”, or the TLD “” from “”.


$components = tldextract('');
echo $components->tld; // Outputs "".


Most people try to do this by splitting the domain name on ‘.’ and assuming that the last component is the TLD, the next-to-last one is the domain, and so on. This works in theory – by definition, the last label in a domain name is the top-level domain. However, in practice you usually want what is know as the “public suffix” or “effective TLD” – the part of the domain name under which Internet users can directly register names.

For example, consider a URL like “”. In this case, “www” is the sub-domain, “darwinhigh” is the domain registered by the user (Darwin High School), and “” is the domain suffix controlled by the registrar. Splitting on the dot character will give you “au” as the TLD and “edu” as the domain name, which is usually not what you want.

On the other hand, tldextract.php uses the Public Suffix List maintained by Mozilla to see what gTLDsccTLDs and domain suffixes are actually in use, and to find out about any TLD- or country-specific exceptions. So it will give you the right answer.

This library a PHP port of the tldextract Python module.


  1. Download
  2. Unpack the archive and move the tldextractphp directory to your “libraries” (or equivalent) directory.
  3. Add this line to the top of your script:
    require '/path/to/tldextract.php';

tldextract.php requires PHP 5.3 or later. It should work on PHP 5.2 as well, but I have not tested it.


$components = tldextract('');
echo $components->subdomain; // www
echo $components->domain;    // bbc
echo $components->tld;       //

Alternatively, you can also access the domain components using array syntax:

$components = tldextract('');
echo $components['tld']; //

Note that the value returned by tldextract() is not a native PHP array, so most array manipulation functions (e.g. implode()) will not work. Use the toArray() method to get the components as an array:

$components = tldextract('');
// Array ( [subdomain] => www [domain] => bbc [tld] => )

Caching And Advanced Usage

This library will automatically attempt to download the latest TLD list from the Public Suffix List when you first run it. It will then cache that list in /path/to/tldextractphp/.tld_set. The cache stays valid indefinitely, so it won’t download the list again unless you manually delete .tld_set.

To prevent this download or choose a different location for the cache file, you will need to create your own TLDExtract instance. The class constructor takes two optional arguments:

  • $fetch – set to true to enable TLD list download, or false to disable. If disabled, the library will fall back to using the included snapshot (.tld_set_snapshot).
  • $cacheFile – set an alternative file name for the TLD list cache.


//Disable live TLD rule set updates. The library will fall back to
//using the included snapshot.
$extract = new TLDExtract(false);
$components = $extract('');

//Store the TLD cache elsewhere.
$extract = new TLDExtract(true, '/path/to/alternative/cache_file');
$components = $extract('');

Running the tests

This library includes a set of PHPUnit tests. To run the tests, open your favourite command-line terminal, navigate to the tldextractphp directory and enter:

phpunit ./tests

Note that the full test suite can take a while to execute. That’s because in addition to normal unit tests, it will also attempt to download the TLD list from Public Suffix List and verify that the local snapshot is up to date. To skip that test, run this instead:

cd tests
phpunit ExtractorTest
Related posts :

9 Responses to “tldextract.php – Extract TLD, Domain And Subdomains From URLs”

  1. Tommy says:

    Hi there,

    seems not to work since the public suffix list has been updated, ie:

  2. Hassan says:

    I’m trying to implemented it and face issue,

    Cannot redeclare tldextract() (previously declared in /home/xxx/public_html/project/inc/tldextract.php:239) in /home/xxx/public_html/project/inc/tldextract.php on line 246

  3. Jānis Elsts says:

    That’s strange. Are you using a compatible PHP version (5.3 or later)? Is there an opcode cache active? In certain rare situations that can cause spurious “cannot redeclare” errors.

  4. Vinod Puliyadi says:

    Good Job!

    I tried with following IDN domain xn--sxqx42c.xn--j6w193g, which not worked properly as expected.

  5. Manfred says:

    It work for me with a simple domain address “domain.tld” instead “some.domain.tld” 😉

  6. Manfred says:

    Edit: How can this work?

    Thank you in advance.

  7. yossi says:

    Thanks a lot!
    But the php source file has no license. Can you update it with available license? Thanks.

  8. Jānis Elsts says:

    Sure. I’ve added the MIT license text to the file.

  9. yossi says:


Leave a Reply