- Notifications
You must be signed in to change notification settings - Fork0
blob-domain is a simple PHP library for parsing and validating domain names.
License
Blobfolio/blob-domain
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
blob-domain is a simple PHP library for parsing and validating domain names. It supports the fullPublic Suffix ruleset, translates Unicode to ASCII or vice versa (if the PHP extension INTL is present), and can break down a host into its constituent parts: subdomain, domain, and suffix.
- ::parse_host()
- ::parse_host_parts()
- is_valid()
- is_ascii()
- is_fqdn()
- is_ip()
- is_unicode()
- has_dns()
- strip_www()
blob-domain and its dependencies require PHP 7.0+ with the following modules:
- BCMath
- DOM
- Fileinfo
- Filter
- JSON
- MBString
- INTL
Use Composer:
composer require"blobfolio/blob-domain:dev-master"
First up, the basics.
// Initialize the object by passing a host-like string.$domain =newblobfolio\domain\domain('www.Google.com');// Access the sanitized host by typecasting as a string.echo (string)$domain;//www.google.com// Get it all. If a part is not applicable, its value will be NULL.$data =$domain->get_data();/*array( host => www.google.com subdomain => www domain => google suffix => com)*/// Or each part using `get_KEY()`.echo$domain->get_host();//www.google.comecho$domain->get_subdomain();//wwwecho$domain->get_domain();//googleecho$domain->get_suffix();//com// By default, Unicode domains are converted to ASCII.$domain =newblobfolio\domain\domain('http://☺.com');echo$domain->get_host();//xn--74h.com// Convert them back by passing `TRUE` to the getters.echo$domain->get_host(true);//☺.com
The following static methods exist if you don't want to initialize an object.
Parse the hostname part of an arbitrary string, which might be a hostname, IP address, URL, etc.
Note: this will convert Unicode to ASCII.
- (string) Host
Returns the processed hostname orFALSE on failure.
$foo =blobfolio\domain\domain::parse_host('http://☺.com');//xn--74h.com$foo =blobfolio\domain\domain::parse_host('co.uk');//FALSE
Pull out the different parts of a host name, i.e. what you'd get runningget_data() on adomain object.
Note: this will convert Unicode to ASCII.
- (string) Host
Returns an array containing the processed parts orFALSE on failure.
$foo =blobfolio\domain\domain::parse_host_parts('www.Google.com');/*array( host => www.google.com subdomain => www domain => google suffix => com)*/
The object also comes with public methods. Aside from the variousget_*() functions already demonstrated, you've got the following.
Is the host valid? This will beTRUE unless:
- The host string is malformed;
- The host string contains Unicode and the INTL extension is missing;
- The host contains no domain portion;
- The
$dnsoption is passed but the host is not a FQDN or is missing anArecord;
- (bool) (optional) Reachable DNS. If
TRUE, the host must either be a public IP address or have anArecord in its DNS table. Default:FALSE
ReturnsTRUE orFALSE.
Whether or not a host is ASCII, likegoogle.com. Unicode domains are still a bit unknown in many parts of the Western world and can cause problems with native PHP functions or databases, so good to know what you've got.
Note: IP addresses are considered ASCII for the purposes of this function.
Note: The variousget_*() functions will always return the host in ASCII format by default. PassingTRUE to those functions will return Unicode hosts in their original Unicode, e.g.☺.com.
N/A
ReturnsTRUE orFALSE.
// ☺.com$domain->is_ascii();//FALSE// xn--74h.com$domain->is_ascii();//FALSE// google.com$domain->is_ascii();//TRUE
Checks to see whether the host is a Fully-Qualified Domain Name (or at least a public IP address). In other words, can it be seen by the outside world?
Note: this does not imply the host actually exists.
N/A
ReturnsTRUE orFALSE.
Is the host an IP address?
- (bool) (optional) Allow restricted/private. Default:
TRUE
ReturnsTRUE orFALSE.
Whether or not a host is Unicode, like☺.com. Unicode hosts can cause problems with native PHP functions and databases, so might be a good thing to know.
Note: The variousget_*() functions will return an ASCIIfied version of a Unicode domain by default, likexn--74h.com. PassingTRUE will de-convert them back to the original Unicode.
N/A
ReturnsTRUE orFALSE.
// ☺.com$domain->is_unicode();//TRUE// xn--74h.com$domain->is_unicode();//TRUE// google.com$domain->is_unicode();//FALSE
Does this host have anA record in its DNS table or is it a public IP address?
Note: theA record cannot point to a restricted/reserved IP like127.0.0.1 or the function will returnFALSE.
N/A
ReturnsTRUE orFALSE.
This removes the leadingwww. subdomain, if any, from the parsed result. You can also have this run automatically at initialization by passing a second argument to the constructor,TRUE.
N/A
ReturnsTRUE orFALSE, indicating whether or not a change was made.
// As separate actions.$foo =newblobfolio\domain\domain('www.Google.com');/*array( host => www.google.com subdomain => www domain => google suffix => com)*/$foo->strip_www();/*array( host => google.com subdomain => NULL domain => google suffix => com)*/// Or you can do this at initializing by passing TRUE as a second argument.$foo =newblobfolio\domain\domain('www.Google.com',true);
Copyright © 2018Blobfolio, LLC <hello@blobfolio.com>
This work is free. You can redistribute it and/or modify it under the terms of the Do What The Fuck You Want To Public License, Version 2.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSEVersion 2, December 2004Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>Everyone is permitted to copy and distribute verbatim or modifiedcopies of this license document, and changing it is allowed as longas the name is changed.DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSETERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION0. You just DO WHAT THE FUCK YOU WANT TO.| If you have found this work useful and would like to contribute financially, Bitcoin tips are always welcome! 1Af56Nxauv8M1ChyQxtBe1yvdp2jtaB1GF |
About
blob-domain is a simple PHP library for parsing and validating domain names.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.