Usage instructions

sabre/uri is a super lightweight package that helps working with URIs. It provides the following functions, all in the Sabre\Uri namespace:


The resolve function allows you to expand a relative URI in a full one, in a similar way a browser would.


namespace Sabre\Uri;

$base = '';
$new = '../hello'

echo Uri\resolve($base, $new);
// Output:

A few more inputs and outputs:

$base $new Output // ?a=b bar
/foo/bar/ ./hi/../../baz /foo/baz
/foo/bar/?query #fragment /foo/bar/?query#fragment


There are many ways to specify URIs that all point to the same thing. The normalize function helps you make these URIs consistent with each other, which in turn allows you to make better comparisons.

For example, these urls all point to the same resource:


namespace Sabre\Uri;

$input = 'HTTP://';

echo Uri\normalize($output);
// Output:


The parse function is a slightly modified version of PHP's parse_url.

The difference is that this function returns the entire array of url parts, even if those parts are not set.


namespace Sabre\Uri;

$input = '/foo/bar';

The above example will output:

    [scheme] =>
    [host] =>
    [path] => '/foo/bar'
    [port] =>
    [user] =>
    [query] =>
    [fragment] =>

Undefined parts are returned as null. This has the benefit that it makes it a lot easier to deal with these arrays in if and ternary statements.


The build function takes the output of parse or parse_url() and creates a new url.

It's basically the reverse operation of parse.


The split function takes a path and returns the basename() and dirname() components.


namespace Sabre\Uri;

$input = "/path/to/file/";

) = Uri\split($input);

echo $parent; // output : /path/to/
echo $basename; // output : file

Why not basename() and dirname()?

  1. The PHP functions are locale-aware and will start behaving differently depending on PHP's locale setting.
  2. The PHP functions will use the backslash as the seperator on Windows.
  3. The PHP functions behave a bit oddly if there is only one path component, which require special casing.