Getting Started
The Swiss Army Knife for Urls
This package is for you when PHP's parse_url()
is not enough.
Key Features:
- Parse a url and access or modify all its components separately.
- Resolve any relative reference you may find in an HTML document to an absolute url, based on the document's url.
- Get not only the full host of a url, but also the registrable domain, the domain suffix and the subdomain parts of the host separately (Thanks to the Mozilla Public Suffix List).
- Compare urls or components of urls (e.g. checking if different urls point to the same host or domain)
- Thanks to true/punycode it's also no problem to parse internationalized domain names (IDN).
- Implements PSR-7 UriInterface.
Installation
Install the latest version with:
composer require crwlr/url
Usage
Including the package
<?php
include('vendor/autoload.php');
use Crwlr\Url\Url;
To start using the library include composer's autoload file and import the Url class so you don't have to write the full namespace path again and again. Further code examples skip the above.
Parsing urls
$url = Url::parse('https://john:123@www.example.com:8080/foo?bar=baz');
// Accessing url components via method calls
$port = $url->port(); // => 8080
$domainSuffix = $url->domainSuffix(); // => "com"
$path = $url->path(); // => "/foo"
$fragment = $url->fragment(); // => NULL
// Or as properties
$scheme = $url->scheme; // => "https"
$user = $url->user; // => "john"
$host = $url->host; // => "www.example.com"
$domain = $url->domain; // => "example.com"