Library to parse domains into their subdomain, SLD, TLD, and registrable domain.
This library is a wrapper around jeremykendall/php-domain-parser
, focussed on ease of use.
Run the following command to install the package from Packagist:
composer require cyberfusion/domain-parser
use Cyberfusion\DomainParser\Parser;
$parser = new Parser();
$parsedDomain = $parser->domain('www.cyberfusion.nl');
$parsedDomain->getRegistrableDomain(); // cyberfusion.nl
$parsedDomain->getSld(); // cyberfusion
$parsedDomain->getTld(); // nl
$parsedDomain->hasSubdomain(): // true
$parsedDomain->getSubdomain(); // www
$parsedDomain->isApexDomain(); // false
$parsedDomain->getFqdn(); // www.cyberfusion.nl
Public Suffix List (recommended)
$parser = new Parser(provider: new PublicSuffixList());
$parser = new Parser(provider: new IANATopLevelDomainList());
This package caches data. to prevent too many requests to providers. You can provide your own cache to Parser
, or use the included file cache.
For example, use the default cache store in Laravel:
$parser = new Parser(
cache: Cache::store(),
provider: new PublicSuffixList()
);