3v4l.org

run code in 300+ PHP versions simultaneously
<?php $path = 'phar://test.phar/a/b/./.././v/../file.txt'; $isUnixPath = ((strlen($path) === 0) || ($path[0] !== '/')); // Checks if path is relative. if ((strpos($path, ':') === false) && ($isUnixPath === true)) { $path = getcwd() . DIRECTORY_SEPARATOR . $path; } // Resolve path parts (single dot, double dot and double delimiters). $path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path); $parts = array_filter(explode(DIRECTORY_SEPARATOR, $path), 'strlen'); $absolutes = array(); foreach ($parts as $part) { if (('.' === $part) || ('' === $part)) { continue; } if ('..' === $part) { array_pop($absolutes); } else { $absolutes[] = $part; } } $path = implode(DIRECTORY_SEPARATOR, $absolutes); // Resolve any symlinks. if ((file_exists($path) === true) && (linkinfo($path) > 0)) { $path = readlink($path); } if ($isUnixPath === false) { $path = '/' . $path; } echo $path;

preferences:
35.37 ms | 402 KiB | 5 Q