26 $delta =
parse($newPath);
28 $pick =
function($part) use (
$base, $delta) {
32 } elseif (
$base[$part]) {
41 if ($delta[
'scheme']) {
47 $newParts[
'scheme'] = $pick(
'scheme');
48 $newParts[
'host'] = $pick(
'host');
49 $newParts[
'port'] = $pick(
'port');
54 if ($delta[
'path'][0] ===
'/') {
55 $path = $delta[
'path'];
59 if (strpos(
$path,
'/') !==
false) {
62 $path .=
'/' . $delta[
'path'];
68 $pathParts = explode(
'/',
$path);
70 foreach ($pathParts as $pathPart) {
77 array_pop($newPathParts);
80 $newPathParts[] = $pathPart;
85 $path = implode(
'/', $newPathParts);
88 $newParts[
'path'] =
$path;
89 if ($delta[
'query']) {
90 $newParts[
'query'] = $delta[
'query'];
91 } elseif (!empty(
$base[
'query']) && empty($delta[
'host']) && empty($delta[
'path'])) {
93 $newParts[
'query'] =
$base[
'query'];
95 if ($delta[
'fragment']) {
96 $newParts[
'fragment'] = $delta[
'fragment'];
98 return build($newParts);
116 $parts =
parse($uri);
118 if (!empty($parts[
'path'])) {
119 $pathParts = explode(
'/', ltrim($parts[
'path'],
'/'));
121 foreach ($pathParts as $pathPart) {
128 array_pop($newPathParts);
132 $newPathParts[] = rawurlencode(rawurldecode($pathPart));
136 $parts[
'path'] =
'/' . implode(
'/', $newPathParts);
139 if ($parts[
'scheme']) {
140 $parts[
'scheme'] = strtolower($parts[
'scheme']);
146 if (!empty($parts[
'port']) && isset($defaultPorts[$parts[
'scheme']]) && $defaultPorts[$parts[
'scheme']] == $parts[
'port']) {
148 unset($parts[
'port']);
151 switch ($parts[
'scheme']) {
154 if (empty($parts[
'path'])) {
156 $parts[
'path'] =
'/';
162 if ($parts[
'host']) $parts[
'host'] = strtolower($parts[
'host']);
164 return build($parts);
188 $uri = preg_replace_callback(
191 return rawurlencode($matches[0]);
226 if (!empty($parts[
'host'])) {
228 if (!empty($parts[
'user'])) {
231 if (!empty($parts[
'port'])) {
236 if (!empty($parts[
'scheme'])) {
238 $uri = $parts[
'scheme'] .
':';
241 if (
$authority || (!empty($parts[
'scheme']) && $parts[
'scheme'] ===
'file')) {
247 if (!empty($parts[
'path'])) {
248 $uri .= $parts[
'path'];
250 if (!empty($parts[
'query'])) {
251 $uri .=
'?' . $parts[
'query'];
253 if (!empty($parts[
'fragment'])) {
254 $uri .=
'#' . $parts[
'fragment'];
282 if (preg_match(
'/^(?:(?:(.*)(?:\/+))?([^\/]+))(?:\/?)$/u',
$path, $matches)) {
283 return [$matches[1], $matches[2]];
309 $uri = preg_replace_callback(
312 return rawurlencode($matches[0]);
327 if (preg_match(
'% ^([A-Za-z][A-Za-z0-9+-\.]+): %x', $uri, $matches)) {
329 $result[
'scheme'] = $matches[1];
331 $uri = substr($uri, strlen($result[
'scheme']) + 1);
336 if (strpos($uri,
'#') !==
false) {
337 list($uri, $result[
'fragment']) = explode(
'#', $uri, 2);
340 if (strpos($uri,
'?') !==
false) {
341 list($uri, $result[
'query']) = explode(
'?', $uri, 2);
344 if (substr($uri, 0, 3) ===
' 345 // The triple slash uris are a bit unusual, but we have special handling 347 $result['path
'] = substr($uri, 2); 348 $result['host
'] = ''; 349 } elseif (substr($uri, 0, 2) === ' 354 (?: (?<user> [^:@]+) (: (?<pass> [^@]+)) @)? 355 (?<host> ( [^:/]* | \[ [^\]]+ \] )) 356 (?: : (?<port> [0-9]+))? 360 if (!preg_match($regex, $uri, $matches)) {
363 if ($matches[
'host']) $result[
'host'] = $matches[
'host'];
364 if ($matches[
'port']) $result[
'port'] = (int)$matches[
'port'];
365 if (isset($matches[
'path'])) $result[
'path'] = $matches[
'path'];
366 if ($matches[
'user']) $result[
'user'] = $matches[
'user'];
367 if ($matches[
'pass']) $result[
'pass'] = $matches[
'pass'];
resolve($basePath, $newPath)
This file contains all the uri handling functions.
split($path)
Returns the 'dirname' and 'basename' for a path.
_parse_fallback($uri)
This function is another implementation of parse_url, except this one is fully written in PHP...
parse($uri)
Parses a URI and returns its individual components.
build(array $parts)
This function takes the components returned from PHP's parse_url, and uses it to generate a new uri...
normalize($uri)
Takes a URI or partial URI as its argument, and normalizes it.