Download the PHP package voku/php-domain-parser without Composer
On this page you can find all versions of the php package voku/php-domain-parser. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download voku/php-domain-parser
More information about voku/php-domain-parser
Files in voku/php-domain-parser
Package php-domain-parser
Short Description Fork: Public Suffix List based URL parsing implemented in PHP.
License MIT
Homepage https://github.com/voku/php-domain-parser
Informations about the package php-domain-parser
PHP Domain Parser
WARNING: this is only a Fork of "https://github.com/jeremykendall/php-domain-parser"
PHP Domain Parser is a Public Suffix List based domain parser implemented in PHP.
Motivation
While there are plenty of excellent URL parsers and builders available, there are very few projects that can accurately parse a url into its component subdomain, registrable domain, and public suffix parts.
Consider the domain www.pref.okinawa.jp. In this domain, the public suffix portion is okinawa.jp, the registrable domain is pref.okinawa.jp, and the subdomain is www. You can't regex that.
Other similar libraries focus primarily on URL building, parsing, and manipulation and additionally include public suffix domain parsing. PHP Domain Parser was built around accurate Public Suffix List based parsing from the very beginning, adding a URL object simply for the sake of completeness.
Installation
The only (currently) supported method of installation is via Composer.
Create a composer.json
file in the root of your project:
And then run: composer install
Add the autoloader to your project:
You're now ready to begin using the PHP Domain Parser.
Usage
Parsing URLs
Parsing URLs into their component parts is as simple as the example you see below.
The above will output:
Convenience Methods
A magic __get()
method is provided to access the above object properties. Obtaining the public
suffix for a parsed domain is as simple as:
IDNA Support
IDN (Internationalized Domain Name)
support was added in version 1.4.0
. Both unicode domains and their ASCII
equivalents are supported.
Unicode
Parsing IDNA hosts is no different that parsing standard hosts. Setting $host = 'Яндекс.РФ';
(Russian-Cyrillic) in the Parsing URLs example would return:
ASCII (Punycode)
If you choose to provide the ASCII equivalent of the unicode domain name
($host = 'http://xn--d1acpjx3f.xn--p1ai';
in the case of the Parsing URLs example),
the ASCII equivalent will be returned by the parser:
IPv6 Support
Parsing IPv6 hosts is no different that parsing standard hosts. Setting $host = 'http://[2001:db8:85a3:8d3:1319:8a2e:370:7348]:8080/';
in the Parsing URLs example would return:
IMPORTANT: IPv6 url host names must be enclosed in square brackets. They will not be parsed properly otherwise.
Hat tip to @geekwright for adding IPv6 support in a bugfix pull request.
Parsing Domains
If you'd like to parse the domain (or host) portion only, you can use
Parser::parseHost()
.
The above will output:
Validation of Public Suffixes
Public Suffix validation is available by calling Parser::isSuffixValid()
:
A suffix is considered invalid if it is not contained in the Public Suffix List.
Huge thanks to @SmellyFish for submitting Add a way to validate TLDs to add public suffix validation to the project.
Retrieving Domain Components Only
If you're only interested in a domain component, you can use the parser to retrieve only the component you're interested in
The above will output:
Sanity Check
You can quickly parse a url from the command line with the provided parse
vendor binary. From the root of your project, simply call:
If you pass a url to parse
, that url will be parsed and the output printed
to screen.
If you do not pass a url, http://user:[email protected]:8080/path/to/page.html?query=string#fragment
will be parsed and the output printed to screen.
Example:
Example Script
For more information on using the PHP Domain Parser, please see the provided example script.
Refreshing the Public Suffix List
While a cached PHP copy of the Public Suffix List is provided for you in the
data
directory, that copy may or may not be up to date (Mozilla provides an
Atom change feed
to keep up with changes to the list). Please use the provided vendor binary to
refresh your cached copy of the Public Suffix List.
From the root of your project, simply call:
You may verify the update by checking the timestamp on the files located in the
data
directory.
Important: The vendor binary update-psl
depends on an internet connection to
update the cached Public Suffix List.
Possible Unexpected Behavior
PHP Domain Parser is built around PHP's
parse_url()
function and, as such, exhibits most
of the same behaviors of that function. Just like parse_url()
, this library
is not meant to validate URLs, but rather to break a URL into its component
parts.
One specific, counterintuitive behavior is that PHP Domain Parser will happily parse a URL with spaces in the host part.
Contributing
Pull requests are always welcome! Please review the CONTRIBUTING.md document before submitting pull requests.
Heads up: BC Break In All 1.4 Versions
The 1.4 series introduced a backwards incompatible change by adding PHP's ext-mbstring
and ext-intl
as dependencies. This should have resulted in a major version
bump. Instead I bumped the minor version from 1.3.1 to 1.4.
I highly recommend reverting to 1.3.1 if you're running into extension issues and
do not want to or cannot install ext-mbstring
and ext-intl
. You will lose
IDNA and IPv6 support, however. Those are only available in versions >= 1.4.
I apologize for any issues you may have encountered due my semver error.
Attribution
The HTTP adapter interface and the cURL HTTP adapter were inspired by (er, lifted from) Will Durand's excellent Geocoder project. His MIT license and copyright notice are below.
Portions of the PublicSuffixListManager and the DomainParser are derivative works of the PHP registered-domain-libs. Those parts of this codebase are heavily commented, and I've included a copy of the Apache Software Foundation License 2.0 in this project.
All versions of php-domain-parser with dependencies
true/punycode Version ~2.1
voku/arrayy Version ~7.4
voku/portable-utf8 Version ~5.4