PHPackages                             panda843/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. panda843/tld-extract

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

panda843/tld-extract
====================

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

2.0.3(3y ago)010Apache-2.0PHPPHP ^5.5.0 || ^7.0 || ^8.0

Since Oct 23Pushed 3y agoCompare

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

READMEChangelog (2)Dependencies (6)Versions (21)Used By (0)

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

32

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity77

Established project with proven stability

 Bus Factor1

Top contributor holds 83.7% 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.

###  Release Activity

Cadence

Every ~127 days

Recently: every ~336 days

Total

20

Last Release

1436d ago

Major Versions

0.2.0 → 1.0.02016-06-20

1.2.7 → 2.0.02018-09-28

PHP version history (4 changes)0.1.1PHP &gt;=5.4

1.0.0PHP &gt;=5.5

1.2.0PHP ^5.5.0 || ^7.0

2.0.3PHP ^5.5.0 || ^7.0 || ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/88f7c9e0ea6ecf147c2ce02534206ae9b43770b36c7ac8b82f2fe87a3b7a22cf?d=identicon)[panda843](/maintainers/panda843)

---

Top Contributors

[![layershifter](https://avatars.githubusercontent.com/u/14183168?v=4)](https://github.com/layershifter "layershifter (77 commits)")[![alexander-schranz](https://avatars.githubusercontent.com/u/1698337?v=4)](https://github.com/alexander-schranz "alexander-schranz (5 commits)")[![panda843](https://avatars.githubusercontent.com/u/7259406?v=4)](https://github.com/panda843 "panda843 (4 commits)")[![Rohaq](https://avatars.githubusercontent.com/u/246865?v=4)](https://github.com/Rohaq "Rohaq (2 commits)")[![Gman98ish](https://avatars.githubusercontent.com/u/7416827?v=4)](https://github.com/Gman98ish "Gman98ish (1 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)")

---

Tags

domain parserTLDExtract

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

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

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

###  Alternatives

[yourls/yourls

Your Own URL Shortener

11.9k27.7k1](/packages/yourls-yourls)[bakame/laravel-domain-parser

Laravel package to integrate PHP Domain parser.

26534.8k4](/packages/bakame-laravel-domain-parser)[esperecyan/url

Makes the algorithms and APIs defined by URL Standard (replaces RFC 3986 and RFC 3987) available on PHP. / URL Standard (RFC 3986、RFC 3987 を置き換える Web 標準仕様) で定義されているアルゴリズム、および API を PHP から利用できるようにします。

158.3k2](/packages/esperecyan-url)[open-web-analytics/owa-php-sdk

The open source analytics framework.

213.0k](/packages/open-web-analytics-owa-php-sdk)

PHPackages © 2026

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