URL utilities
Inphinit\Utility\Url is a utility for normalizing URLs, improving path readability (slug), and resolving dots and redundant slashes.
Parsing URL 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; // http
$host = $url->host; // example.com
$path = $url->path; // /foo bar/baz.html
$query = $url->query; // Z=1&B=2&C=3&Y=4
$fragment = $url->fragment; // fragment
$port = $url->port; // NULL
$user = $url->user; // NULL
$pass = $url->pass; // NULL
$output = (string) $url; // unparse url
echo $output;
Output:
http://example.com/foo+bar/baz.html?Z=1&B=2&C=3&Y=4#fragment
Parsing path:
use Inphinit\Utility\Url;
$path = '/characters/Félix the Cat';
$url = new Url($path);
$scheme = $url->scheme; // NULL
$host = $url->host; // NULL
$path = $url->path; // /characters/Félix the Cat"
$query = $url->query; // NULL
$fragment = $url->fragment; // NULL
$port = $url->port; // NULL
$user = $url->user; // NULL
$pass = $url->pass; // NULL
$output = (string) $url; // unparse url
echo $output;
Output:
/characters/F%C3%A9lix+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
Normalize URLs
| Constant | Description |
|---|---|
Url::PATH_ASCII |
Convert the path to ASCII |
Url::PATH_UNICODE |
Convert the path to lower unicode |
Url::PATH_SLUG |
Convert spaces, underscore to scapes and remove unused characteres. Note: @ is converted to -at- |
Url::SORT_QUERY |
Sort querystring |
use Inphinit\Utility\Url;
$str = "http://localhost:9000/FOO/../BAR/./ã_é_ô_ü@hello/Αλφαβητικός?Z=1&B=2&C=3&Y=4";
$url = new Url($str);
$url->normalize();
echo $url, "
\n";
echo "
\n";
$url = new Url($str);
$url->normalize(Url::PATH_ASCII);
echo $url, "
\n";
$url = new Url($str);
$url->normalize(Url::PATH_UNICODE);
echo $url, "
\n";
$url = new Url($str);
$url->normalize(Url::PATH_SLUG);
echo $url, "
\n";
$str = "http://localhost?Z=1&B=2&C=3&Y=4";
$url = new Url($str);
$url->normalize(Url::SORT_QUERY);
echo $url;
Output:
http://localhost:9000/BAR/%C3%A3_%C3%A9_%C3%B4_%C3%BC@hello/%CE%91%CE%BB%CF%86%CE%B1%CE%B2%CE%B7%CF%84%CE%B9%CE%BA%CF%8C%CF%82?Z=1&B=2&C=3&Y=4
http://localhost:9000/bar/a_e_o_u@hello/alphabetikos?Z=1&B=2&C=3&Y=4
http://localhost:9000/bar/%C3%A3_%C3%A9_%C3%B4_%C3%BC@hello/%CE%B1%CE%BB%CF%86%CE%B1%CE%B2%CE%B7%CF%84%CE%B9%CE%BA%CF%8C%CF%82?Z=1&B=2&C=3&Y=4
http://localhost:9000/BAR/%C3%A3-%C3%A9-%C3%B4-%C3%BC-at-hello/%CE%91%CE%BB%CF%86%CE%B1%CE%B2%CE%B7%CF%84%CE%B9%CE%BA%CF%8C%CF%82?Z=1&B=2&C=3&Y=4
http://localhost?B=2&C=3&Y=4&Z=1
Using multiple configurations
use Inphinit\Utility\Url;
$str = "http://example/foo/../--x--/--/./ã é ô ü/?B=2&C=3&Y=4&Z=1";
$url = new Url($str);
$url->normalize();
echo $url, "
\n";
$url = new Url($str);
$url->normalize(Url::PATH_UNICODE|Url::PATH_SLUG|Url::SORT_QUERY);
echo $url;
Output:
http://example/--x--/--/%C3%A3+%C3%A9+%C3%B4+%C3%BC/?B=2&C=3&Y=4&Z=1
http://example/x/%C3%A3-%C3%A9-%C3%B4-%C3%BC/?B=2&C=3&Y=4&Z=1
Normalizing only the path
$url = new Url('/foo/../bar/./á é í/user@localhost/Αλφαβητικός/');
$url->normalize(Url::PATH_ASCII|Url::PATH_SLUG);
echo $url;
Output:
/bar/a-e-i/user-at-localhost/alphabetikos/
Normalizing paths in the format used in Windows
$url = new Url('C:\\foo\\..\bar\\.\á é í\\userlocalhost\\Αλφαβητικός\\');
$url->normalize();
echo $url;
Output:
file:///C:%5Cbar%5C%C3%A1+%C3%A9+%C3%AD%5Cuserlocalhost%5C%CE%91%CE%BB%CF%86%CE%B1%CE%B2%CE%B7%CF%84%CE%B9%CE%BA%CF%8C%CF%82%5C
Normalizing mailto URL
$url = new Url('mailto:섭지코지@Αλφαβητικός.io?subject=This is the+subject&cc=someone_else@example.com&body=This is the+body http://example.io/2000/svg');
$url->normalize();
echo $url;
Output:
mailto:%EC%84%AD%EC%A7%80%EC%BD%94%EC%A7%80@%CE%91%CE%BB%CF%86%CE%B1%CE%B2%CE%B7%CF%84%CE%B9%CE%BA%CF%8C%CF%82.io?subject=This+is+the%2Bsubject&cc=someone_else@example.com&body=This+is+the%2Bbody+http://example.io/2000/svg
Resolving paths
Url::canonpath() resolves references to /./, /../ and extra / characters in the input path and returns the canonicalized absolute pathname. It's similar to the native realpath() function, but it doesn't require the file to exist.
Resolving a path
use Inphinit\Utility\Url;
echo Url::canonpath('/home/foo/../bar/./test.txt');
Output:
/home/bar/test.txt
Resolving a Windows path
use Inphinit\Utility\Url;
echo Url::canonpath('C:\\home\\foo\\..\\bar\\.\\test.txt');
Output:
C:\home\bar\test.txt