@ 2014-03-21T16:26:24Z <?php
/**
* Normalise a path, resolving empty, . and .. components, optionally against another path
*
* @param string $path
* @param string $target
* @return string
*/
function resolve_path($path, $relativeBase = null)
{
// Find separator in use and determine whether output path should be absolute
$separator = strpos($path, '\\') !== false || strpos($relativeBase, '\\') !== false ? '\\' : '/';
$isAbsolute = (!isset($relativeBase) && ($path[0] === '/' || $path[0] === '\\'))
|| (isset($relativeBase) && ($relativeBase[0] === '/' || $relativeBase[0] === '\\'));
// Create the base output array
$target = $relativeBase !== null ? preg_split('#[\\\\/]+#', $relativeBase, -1, PREG_SPLIT_NO_EMPTY) : [];
// Strip empty components and resolve . and ..
foreach (preg_split('#[\\\\/]+#', $path, -1, PREG_SPLIT_NO_EMPTY) as $component) {
switch ($component) {
case '.': // current directory - do nothing
break;
case '..': // up a level
array_pop($target);
break;
default:
$target[] = $component;
break;
}
}
// Add a trailing empty element if path refers to a directory
$lastChar = $path[strlen($path) - 1];
if ($lastChar === '/' || $lastChar === '\\') {
$target[] = '';
}
// Add a leading slash if path is absolute
if ($isAbsolute) {
if ($target) {
array_unshift($target, '');
} else {
$target = ['', ''];
}
}
return implode($separator, $target);
}
$tests = [
'/foo/bar/./../baz' => null,
'/foo/bar/./../../baz/..' => null,
'/foo/bar/' => '/baz/qux',
'../..' => '/foo/bar/baz/qux',
'.' => '/foo/bar/baz/qux',
];
foreach ($tests as $path => $base) {
$result = resolve_path($path, $base);
echo "
Path: $path
Base: $base
Result: $result
";
}
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 8.1.0 - 8.1.28 , 8.2.0 - 8.2.18 , 8.3.0 - 8.3.6 Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /in/rufA9 on line 13
Path: /foo/bar/./../baz
Base:
Result: /foo/baz
Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /in/rufA9 on line 13
Path: /foo/bar/./../../baz/..
Base:
Result: /
Path: /foo/bar/
Base: /baz/qux
Result: /baz/qux/foo/bar/
Path: ../..
Base: /foo/bar/baz/qux
Result: /foo/bar
Path: .
Base: /foo/bar/baz/qux
Result: /foo/bar/baz/qux
Output for 5.4.0 - 5.4.45 , 5.5.24 - 5.5.35 , 5.6.8 - 5.6.28 , 7.0.0 - 7.0.20 , 7.1.0 - 7.1.20 , 7.2.0 - 7.2.33 , 7.3.12 - 7.3.33 , 7.4.0 - 7.4.33 , 8.0.0 - 8.0.30 Path: /foo/bar/./../baz
Base:
Result: /foo/baz
Path: /foo/bar/./../../baz/..
Base:
Result: /
Path: /foo/bar/
Base: /baz/qux
Result: /baz/qux/foo/bar/
Path: ../..
Base: /foo/bar/baz/qux
Result: /foo/bar
Path: .
Base: /foo/bar/baz/qux
Result: /foo/bar/baz/qux
Output for 4.4.2 - 4.4.9 , 5.1.0 - 5.1.6 , 5.2.0 - 5.2.17 , 5.3.0 - 5.3.29 Parse error: syntax error, unexpected '[' in /in/rufA9 on line 18
Process exited with code 255 . Output for 4.3.0 - 4.3.1 , 4.3.5 - 4.3.11 , 4.4.0 - 4.4.1 , 5.0.0 - 5.0.5 Parse error: parse error, unexpected '[' in /in/rufA9 on line 18
Process exited with code 255 . Output for 4.3.2 - 4.3.4 Parse error: parse error in /in/rufA9 on line 18
Process exited with code 255 . preferences:dark mode live preview
254.06 ms | 401 KiB | 314 Q