https://github.com/bpolaszek/hostname-extractor
A standalone PHP library to parse hostnames.
https://github.com/bpolaszek/hostname-extractor
domain host hostname php uri url
Last synced: 11 months ago
JSON representation
A standalone PHP library to parse hostnames.
- Host: GitHub
- URL: https://github.com/bpolaszek/hostname-extractor
- Owner: bpolaszek
- License: mit
- Created: 2017-12-07T17:17:36.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2019-04-23T14:43:01.000Z (over 6 years ago)
- Last Synced: 2025-02-11T22:48:26.087Z (11 months ago)
- Topics: domain, host, hostname, php, uri, url
- Language: PHP
- Homepage:
- Size: 17.6 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://packagist.org/packages/bentools/hostname-extractor)
[](https://packagist.org/packages/bentools/hostname-extractor)
[](https://travis-ci.org/bpolaszek/hostname-extractor)
[](https://coveralls.io/github/bpolaszek/hostname-extractor?branch=master)
[](https://scrutinizer-ci.com/g/bpolaszek/hostname-extractor)
[](https://packagist.org/packages/bentools/hostname-extractor)
# Hostname Extractor
A simple library to manipulate domains.
## Usage
```php
require_once __DIR__ . '/vendor/autoload.php';
use BenTools\HostnameExtractor\HostnameExtractor;
use BenTools\HostnameExtractor\SuffixProvider\PublicSuffixProvider;
$extractor = new HostnameExtractor(new PublicSuffixProvider());
$hostname = $extractor->extract('my.preferred.domain.co.uk');
dump($hostname->getSubdomain()); // my.preferred
dump($hostname->getDomain()); // domain
dump($hostname->getSuffix()); // co.uk
dump($hostname->getTld()); // uk
dump($hostname->getSuffixedDomain()); // domain.co.uk
dump($hostname->isIp()); // false
dump($hostname->isIpv4()); // false
dump($hostname->isIpv6()); // false
```
As you can see, `co.uk` is considered as a suffix (otherwise, we would have considered that `co` is the domain and `uk` the suffix / tld).
The library parses the [Public Suffix List](https://publicsuffix.org/), which stores thousands of existing suffixes.
To avoid network latency feel free to implement your own `BenTools\HostnameExtractor\SuffixProvider\SuffixProviderInterface` or to use your favorite [PSR-16](http://www.php-fig.org/psr/psr-16/) library into a `BenTools\HostnameExtractor\SuffixProvider\PSR16CacheSuffixProvider` object.
## Installation
PHP 7.1+ with `mbstring` extension is required.
> composer require bentools/hostname-extractor ^1.0
## Tests
> ./vendor/bin/phpunit
## License
MIT.
## See also
[bentools/querystring](https://github.com/bpolaszek/querystring) - Easily manipulate your query strings.
[bentools/uri-factory](https://github.com/bpolaszek/uri-factory) - PSR-7 `UriInterface` factory with multiple libaries support.