PHPackages                             showapp/tld-extract - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. showapp/tld-extract

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

showapp/tld-extract
===================

TLDExtract, library for extracting parts of domain, e.q. domain parser

021PHP

Since Feb 17Pushed 3y agoCompare

[ Source](https://github.com/ShowappSoftware/TLDExtract)[ Packagist](https://packagist.org/packages/showapp/tld-extract)[ RSS](/packages/showapp-tld-extract/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

DEPRECATED
==========

[](#deprecated)

Consider to use  as maintained alternative.

TLDExtract
==========

[](#tldextract)

`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](https://camo.githubusercontent.com/0ac2ae7a7d2b29422ebf4b66b692fdc224e362b0d8695e849a59e634fd228f44/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6c61796572736869667465722f746c642d657874726163742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/layershifter/tld-extract)[![Software License](https://camo.githubusercontent.com/a3156ec43c89f96e604aba487fd0cbbc4b0f849a5b8e5b91405bc4476d275d72/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d417061636865322d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/3777ffc7e3b13478087cb5fbb220db0a03db4dc2261cd6bbddcf6a75b0573e4d/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6c61796572736869667465722f544c44457874726163742f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/layershifter/TLDExtract)[![Coverage Status](https://camo.githubusercontent.com/7e2d27445600610aa73ef7f8436b7038444d8fbb9b33f6424fb61f673748c572/68747470733a2f2f636f6465636f762e696f2f67682f6c61796572736869667465722f544c44457874726163742f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/layershifter/TLDExtract)[![Total Downloads](https://camo.githubusercontent.com/8c93e6e696e6df1d2902a1ac47fbf6fb06ee9d94336398fe6ce68a5b620577e9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6c61796572736869667465722f746c642d657874726163742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/layershifter/tld-extract)

---

*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 'co.uk' 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](http://www.publicsuffix.org). So, given a URL, it knows its subdomain from its domain, and its domain from its country code.

```
$result = tld_extract('http://forums.news.cnn.com/');
var_dump($result);

object(LayerShifter\TLDExtract\Result)#34 (3) {
  ["subdomain":"LayerShifter\TLDExtract\Result":private]=>
  string(11) "forums.news"
  ["hostname":"LayerShifter\TLDExtract\Result":private]=>
  string(3) "cnn"
  ["suffix":"LayerShifter\TLDExtract\Result":private]=>
  string(3) "com"
}
```

`Result` implements ArrayAccess interface, so you simple can access to its result.

```
var_dump($result['subdomain']);
string(11) "forums.news"
var_dump($result['hostname']);
string(3) "cnn"
var_dump($result['suffix']);
string(3) "com"
```

Also you can simply convert result to JSON.

```
var_dump($result->toJson());
string(54) "{"subdomain":"forums.news","hostname":"cnn","suffix":"com"}"
```

This package is compliant with [PSR-1](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-1-basic-coding-standard.md), [PSR-2](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md), [PSR-4](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader.md). If you notice compliance oversights, please send a patch via pull request.

### Does TLDExtract make requests to Public Suffix List website?

[](#does-tldextract-make-requests-to-public-suffix-list-website)

No. `TLDExtract` uses database from [TLDDatabase](https://github.com/layershifter/TLDDatabase) that generated from Public Suffix List and updated regularly. It does not make any HTTP requests to parse or validate a domain.

Requirements
------------

[](#requirements)

The following versions of PHP are supported.

- PHP 5.5
- PHP 5.6
- PHP 7.0
- PHP 7.1
- PHP 7.2
- PHP 7.3
- HHVM

Install
-------

[](#install)

Via Composer

```
$ composer require layershifter/tld-extract
```

Additional result methods
-------------------------

[](#additional-result-methods)

Class `LayerShifter\TLDExtract\Result` has some usable methods:

```
$extract = new LayerShifter\TLDExtract\Extract();

# For domain 'shop.github.com'

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

# For IP '192.168.0.1'

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

Custom database
---------------

[](#custom-database)

By default package is using database from [TLDDatabase](https://github.com/layershifter/TLDDatabase) package, but you can override this behaviour simply:

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

For more details and how keep database updated [TLDDatabase](https://github.com/layershifter/TLDDatabase).

Implement own result
--------------------

[](#implement-own-result)

By default after parse you will receive object of `LayerShifter\TLDExtract\Result` class, but sometime you need own methods or additional functionality.

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

```
class CustomResult implements LayerShifter\TLDExtract\ResultInterface {}

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

Parsing modes
-------------

[](#parsing-modes)

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 LayerShifter\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
----------

[](#change-log)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

Testing
-------

[](#testing)

```
$ composer test
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) and [CONDUCT](CONDUCT.md) for details.

License
-------

[](#license)

This library is released under the Apache 2.0 license. Please see [License File](LICENSE) for more information.

###  Health Score

16

—

LowBetter than 5% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity23

Early-stage or recently created project

 Bus Factor1

Top contributor holds 82.8% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/10a86f62700463c33136d05df7b21b2c4608ef2b0ab8ecdf9befc45e4593cffd?d=identicon)[showapp](/maintainers/showapp)

---

Top Contributors

[![layershifter](https://avatars.githubusercontent.com/u/14183168?v=4)](https://github.com/layershifter "layershifter (77 commits)")[![ShowappSoftware](https://avatars.githubusercontent.com/u/49631525?v=4)](https://github.com/ShowappSoftware "ShowappSoftware (5 commits)")[![alexander-schranz](https://avatars.githubusercontent.com/u/1698337?v=4)](https://github.com/alexander-schranz "alexander-schranz (5 commits)")[![Rohaq](https://avatars.githubusercontent.com/u/246865?v=4)](https://github.com/Rohaq "Rohaq (2 commits)")[![laszlof](https://avatars.githubusercontent.com/u/1486486?v=4)](https://github.com/laszlof "laszlof (1 commits)")[![Erwane](https://avatars.githubusercontent.com/u/712604?v=4)](https://github.com/Erwane "Erwane (1 commits)")[![scrutinizer-auto-fixer](https://avatars.githubusercontent.com/u/6253494?v=4)](https://github.com/scrutinizer-auto-fixer "scrutinizer-auto-fixer (1 commits)")[![Gman98ish](https://avatars.githubusercontent.com/u/7416827?v=4)](https://github.com/Gman98ish "Gman98ish (1 commits)")

### Embed Badge

![Health badge](/badges/showapp-tld-extract/health.svg)

```
[![Health](https://phpackages.com/badges/showapp-tld-extract/health.svg)](https://phpackages.com/packages/showapp-tld-extract)
```

###  Alternatives

[dmind/cookieman

Provides a user consent popup. It asks for approval to include tracking objects (cookies, images or any HTML) and includes the objects when consented. It also removes cookies after the consent has been revoked.

39190.5k](/packages/dmind-cookieman)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
