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:

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"