Skip to main content
Github

URL utilities

The Inphinit\Utility\Url class is a utility for processing browser-friendly URLs, also offering features to modify the URL and make it more user-friendly, such as slug formatting.

Parsing URLs and paths

Parsing URL:

use Inphinit\Utility\Url; $path = 'http://example.com/foo bar/baz.html?Z=1&B=2&C=3&Y=4#fragment'; $url = new Url($path); $scheme = $url->scheme; // Output: http $host = $url->host; // Output: example.com $path = $url->path; // Output: /foo bar/baz.html $query = $url->query; // Output: array(Z => 1, B => 2, C => 3, Y => 4) $fragment = $url->fragment; // Output: fragment $port = $url->port; // Output: NULL $user = $url->user; // Output: NULL $pass = $url->pass; // Output: NULL $output = (string) $url; // Output: unparse url echo $output;

Output:

http://example.com/foo bar/baz.html?Z=1&B=2&C=3&Y=4#fragment

Parsing only paths:

use Inphinit\Utility\Url; $path = '/characters/Félix the Cat'; $url = new Url($path); $scheme = $url->scheme; // Output: NULL $host = $url->host; // Output: NULL $path = $url->path; // Output: /characters/Félix the Cat" $query = $url->query; // Output: array(Z => 1, B => 2, C => 3, Y => 4) $fragment = $url->fragment; // Output: NULL $port = $url->port; // Output: NULL $user = $url->user; // Output: NULL $pass = $url->pass; // Output: NULL $output = (string) $url; // Output: unparse url echo $output;

Output:

/characters/Félix the Cat

Modifying the components of a URL:

use Inphinit\Utility\Url; $path = 'http://example.com/foo bar/baz.html?Z=1&B=2&C=3&Y=4#fragment'; $url = new Url($path); $url->path = '/halloween/boo.php'; $url->host = 'localhost'; $url->scheme = 'https'; $url->port = '9090'; $url->user = 'john'; $url->pass = 'f8ySCTgD'; $url->fragment = null; $url->query = ['a' => 'b']; echo $url;

Output:

https://john:f8ySCTgD@localhost:9090/halloween/boo.php?a=b

Modifying the structure of URLs

When using the method Inphinit\Utility\Url::modify() , a new instance will be created, containing the settings based on the flags:

Constant Description
Url::HOST_IDNA_ASCII Modifies the host of a URL to the IDNA format in a new instance.
Url::PATH_ASCII Modifies the path of a URL to ASCII in a new instance.
Url::PATH_RESOLVE Modifies the path of a URL by resolving .. it . in a new instance.
Url::PATH_SLUG Modifies the path of a URL by replacing spaces, underscores, and slashes, removing unused characters, and creating a new instance. Note: @ it is converted to -at-
Url::PATH_UNICODE Modifies the path of a URL to lowercase Unicode in a new instance.
Url::SORT_QUERY Modifies the querystring of a URL, ordering its values in a new instance.

Some examples of use:

use Inphinit\Utility\Url; $input = 'http://localhost:9000/FOO/../BAR/./ã_é_ô_ü@hello/Αλφαβητικός?Z=1&B=2&C=3&Y=4'; $url = new Url($input); $modified = $url->modify(Url::PATH_ASCII); // Output: http://localhost:9000/foo/../bar/./a_e_o_u@hello/alphabetikos?Z=1&B=2&C=3&Y=4 echo $modified; $modified = $url->modify(Url::PATH_UNICODE); // Output: http://localhost:9000/foo/../bar/./ã_é_ô_ü@hello/αλφαβητικός?Z=1&B=2&C=3&Y=4 echo $modified; $modified = $url->modify(Url::PATH_SLUG); // Output: http://localhost:9000/FOO/BAR/ã-é-ô-ü-at-hello/Αλφαβητικός?Z=1&B=2&C=3&Y=4 echo $modified; $modified = $url->modify(Url::PATH_RESOLVE); // Output: http://localhost:9000/BAR/ã_é_ô_ü@hello/Αλφαβητικός?Z=1&B=2&C=3&Y=4 echo $modified; $modified = $url->modify(Url::SORT_QUERY); // Output: http://localhost:9000/FOO/../BAR/./ã_é_ô_ü@hello/Αλφαβητικός?B=2&C=3&Y=4&Z=1 echo $modified; $modified = $url->modify(Url::PATH_UNICODE|Url::PATH_RESOLVE|Url::PATH_SLUG|Url::SORT_QUERY); // Output: http://localhost:9000/bar/ã-é-ô-ü-at-hello/αλφαβητικός?B=2&C=3&Y=4&Z=1 echo $modified; $modified = $url->modify(Url::PATH_ASCII|Url::PATH_RESOLVE|Url::PATH_SLUG|Url::SORT_QUERY); // Output: http://localhost:9000/bar/a-e-o-u-at-hello/alphabetikos?B=2&C=3&Y=4&Z=1 echo $modified;

Using multiple configurations

use Inphinit\Utility\Url; $input = 'http://localhost:9000/FOO/../BAR/./ã_é_ô_ü@hello/Αλφαβητικός?Z=1&B=2&C=3&Y=4'; $url = new Url($input); $modified = $url->modify(Url::PATH_UNICODE|Url::PATH_RESOLVE|Url::PATH_SLUG|Url::SORT_QUERY); // Output: http://localhost:9000/bar/ã-é-ô-ü-at-hello/αλφαβητικός?B=2&C=3&Y=4&Z=1 echo $modified; $modified = $url->modify(Url::PATH_ASCII|Url::PATH_RESOLVE|Url::PATH_SLUG|Url::SORT_QUERY); // Output: http://localhost:9000/bar/a-e-o-u-at-hello/alphabetikos?B=2&C=3&Y=4&Z=1 echo $modified;

Modifying only the path of a URL

$url = new Url('/foo/../bar/./á é í/user@localhost/Αλφαβητικός/'); $modified = $url->modify(Url::PATH_ASCII|Url::PATH_SLUG); // Output: /bar/á-é-í/userlocalhost/Αλφαβητικός/ echo $modified;

Modifying Windows paths

$url = new Url('C:\\foo\\..\\bar\\.\\á é í\\userlocalhost\\Αλφαβητικός\\'); $modified = $url->modify(Url::PATH_RESOLVE|Url::PATH_SLUG); // Output: file:///C:\bar\á-é-í\userlocalhost\Αλφαβητικός\ echo $modified;

Modifying domain to IDNA ASCII form (UTS #46)

$url = new Url('https://你好.com/foo/bar'); $modified = $url->modify(Url::HOST_IDNA_ASCII); // Output: https://xn--6qq79v.com/foo/bar echo $modified;