TLDExtract, library for parsing and extracting the parts of a domain name

2.0.0 2016-12-14 22:13 UTC

This package is not auto-updated.

Last update: 2020-06-26 19:37:20 UTC


TLDExtract accurately separates the gTLD or ccTLD (generic or country code top-level domain) from the registered domain and subdomains of a URL, e.g. domain parser. For example, say you want just the 'google' part of ''.

Latest Version on Packagist

Everybody gets this wrong. Splitting on the '.' and taking the last 2 elements goes a long way only if you're thinking of simple e.g. .com domains. Think parsing for example: the naive splitting method above will give you 'co' as the domain and 'uk' as the TLD, instead of 'bbc' and '' respectively.

TLDExtract on the other hand knows what all gTLDs and ccTLDs look like by looking up the currently living ones according to the Public Suffix List. So, given a URL, it knows its subdomain from its domain, and its domain from its country code.

$result = tld_extract('');
object(CrowdTwist\TLDExtract\Result)#34 (3) {
  string(11) ""
  string(3) "cnn"
  string(3) "com"

Result implements the ArrayAccess interface, so access its result simply.

string(11) ""
string(3) "cnn"
string(3) "com"

Also you can easily convert the result to JSON.

string(54) "{"hostname":"","subdomain":"cnn","suffix":"com"}"

Does TLDExtract make requests to Public Suffix List website?

No. TLDExtract uses the database from TLDDatabase, which is generated from the Public Suffix List and updated regularly. It does not make any HTTP requests to parse or validate a domain.


The following versions of PHP are supported.

  • PHP 5.5
  • PHP 5.6
  • PHP 7.0
  • PHP 7.1
  • HHVM


Via Composer

$ composer require crowdtwist/tld-extract

Additional result methods

Class CrowdTwist\TLDExtract\Result has some usable methods:

use CrowdTwist\TLDExtract\Extract as DomainExtractor;
$extract = new DomainExtractor;

# For domain ''

$result = $extract->parse('');
$result->getFullHost(); // will return (string) ''
$result->getRegistrableDomain(); // will return (string) ''
$result->getPublicDomain(); // will return (string) ''
$result->isValidDomain(); // will return (bool) true
$result->isIp(); // will return (bool) false

# For IP ''

$result = $extract->parse('');
$result->getFullHost(); // will return (string) ''
$result->getRegistrableDomain(); // will return null
$result->getPublicDomain(); // will return null
$result->isValidDomain(); // will return (bool) false
$result->isIp(); // will return (bool) true

Custom database

By default the package uses the database from TLDDatabase package, but you can override this behaviour with:

new CrowdTwist\TLDExtract\Extract(__DIR__ . '/cache/mydatabase.php');

For more details and information on how to keep the database updated, see TLDDatabase.

Implementing your own result

By default, parsing will return a CrowdTwist\TLDExtract\Result object, but sometimes you need own methods or additional functionality.

You can create your own class that implements CrowdTwist\TLDExtract\ResultInterface and use it as parse result.

class CustomResult implements CrowdTwist\TLDExtract\ResultInterface

new CrowdTwist\TLDExtract\Extract(null, CustomResult::class);

Parsing modes

The package has three modes of parsing:

  • allow ICANN suffixes (domains are those delegated by ICANN or part of the IANA root zone database);
  • allow private domains (domains are amendments submitted to Public Suffix List by the domain holder, as an expression of how they operate their domain security policy);
  • allow custom (domains that are not in list, but can be usable, for example: example, mycompany, etc).

For keeping compatibility with Public Suffix List ideas package runs in all these modes by default, but you can easily change this behavior:

use CrowdTwist\TLDExtract\Extract;

new Extract(null, null, Extract::MODE_ALLOW_ICANN);
new Extract(null, null, Extract::MODE_ALLOW_PRIVATE);
new Extract(null, null, Extract::MODE_ALLOW_NOT_EXISTING_SUFFIXES);
new Extract(null, null, Extract::MODE_ALLOW_ICANN | Extract::MODE_ALLOW_PRIVATE);

Change log

Please see CHANGELOG for more information what has changed recently.


$ composer test


Please see CONTRIBUTING and CONDUCT for details.


This library is released under the Apache 2.0 license. Please see License File for more information.