Overview¶
Url¶
A Url
models a URL, providing component access
and modification through the PSR7 UriInterface.
<?php
use webignition\Url\Url;
$url = new Url('http://example.com/path?query#fragment');
$url->getScheme();
// "http"
$url->getQuery();
// "query"
$modifiedUrl = $url
->withScheme('https')
->withPath('/modified-path')
->withQuery('foo=bar')
->withFragment('');
(string) $modifiedUrl;
// https://example.com/modified-path?foo=bar
Read the Url usage guide for more detail.
Normalizer¶
The Normalizer
can apply any combination of sixteen normalizations to any UriInterface
implementation.
<?php
use webignition\Url\Normalizer;
$url = new Url('http://example.com/path?c=cow&a=apple&b=bear#fragment');
$normalizedUrl = Normalizer::normalize(
$url,
Normalizer::SORT_QUERY_PARAMETERS | Normalizer::REMOVE_FRAGMENT
);
(string) $normalizedUrl;
// "http://example.com/path?a=apple&b=bear&c=cow"
Flags:
- capitalize percent encoding
- decode unreserved characters
- convert empty http path
- remove default file host
- remove port host
- remove path dot segments
- convert host unicode to punycode
- reduce duplicate path slashes
- sort query parameters
- add path trailing slash-
- remove user info
- remove fragment
- remove www sub-domain
Options:
Read the Normalizer usage guide for more detail.
Parser¶
The Parser
transforms a URL string into an array of component parts. Useful for direct access to raw URL components.
The parser is used internally by Url::create()
and isn’t needed for normal URL usage.
<?php
use webignition\Url\Parser;
$components = Parser::parse('https://example.com:8080/path?query#fragment');
$components[Parser::COMPONENT_SCHEME];
// "https"
$components[Parser::COMPONENT_HOST];
// "example.com"