3v4l.org

run code in 300+ PHP versions simultaneously
<?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) { array_unshift($target, ''); } return implode($separator, $target); } $tests = [ '/foo/bar/./../baz' => null, '/foo/bar/./../../baz/..' => null, '/foo/bar/./../baz' => null, ]; foreach ($tests as $path => $base) { $result = resolve_path($path, $base); echo " Path: $path Base: $base Result: $result "; }
Output for git.master, git.master_jit, rfc.property-hooks
Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /in/dZ6iT 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/dZ6iT on line 13 Path: /foo/bar/./../../baz/.. Base: Result:

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
45.02 ms | 402 KiB | 8 Q