3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Remove dot segments from a URI path according to RFC3986 Section 5.2.4 * * @param $path * @return string * @link http://www.ietf.org/rfc/rfc3986.txt */function removeDotPathSegments($path) { if (strpos($path, '.') === false) { return $path; }  $inputBuffer = $path; $outputStack = [];  /** * 2. While the input buffer is not empty, loop as follows: */ while ($inputBuffer != '') { /** * A. If the input buffer begins with a prefix of "../" or "./", * then remove that prefix from the input buffer; otherwise, */ if (strpos($inputBuffer, "./") === 0) { $inputBuffer = substr($inputBuffer, 2); continue; } if (strpos($inputBuffer, "../") === 0) { $inputBuffer = substr($inputBuffer, 3); continue; }  /** * B. if the input buffer begins with a prefix of "/./" or "/.", * where "." is a complete path segment, then replace that * prefix with "/" in the input buffer; otherwise, */ if ($inputBuffer === "/.") { $outputStack[] = '/'; break; } if (substr($inputBuffer, 0, 3) === "/./") { $inputBuffer = substr($inputBuffer, 2); continue; }  /** * C. if the input buffer begins with a prefix of "/../" or "/..", * where ".." is a complete path segment, then replace that * prefix with "/" in the input buffer and remove the last * segment and its preceding "/" (if any) from the output * buffer; otherwise, */ if ($inputBuffer === "/..") { array_pop($outputStack); $outputStack[] = '/'; break; } if (substr($inputBuffer, 0, 4) === "/../") { array_pop($outputStack); $inputBuffer = substr($inputBuffer, 3); continue; }  /** * D. if the input buffer consists only of "." or "..", then remove * that from the input buffer; otherwise, */ if ($inputBuffer === '.' || $inputBuffer === '..') { break; }  /** * E. move the first path segment in the input buffer to the end of * the output buffer, including the initial "/" character (if * any) and any subsequent characters up to, but not including, * the next "/" character or the end of the input buffer. */ if (($slashPos = stripos($inputBuffer, '/', 1)) === false) { $outputStack[] = $inputBuffer; break; } else { $outputStack[] = substr($inputBuffer, 0, $slashPos); $inputBuffer = substr($inputBuffer, $slashPos); } }  return implode($outputStack);}  // --- Test --- $expectations = [ ['bar', '....//bar'], ['bar', './bar'], ['bar', '.././bar'], ['bar', '.././bar'], ['/foo/bar', '/foo/./bar'], ['/bar/', '/bar/./'], ['/', '/.'], ['/bar/', '/bar/.'], ['/bar', '/foo/../bar'], ['/', '/bar/../'], ['/', '/..'], ['/', '/bar/..'], ['/foo/', '/foo/bar/..'], ['', '.'], ['', '..'],];  foreach ($expectations as $struct) { list($expected, $input) = $struct; $result = removeDotPathSegments($input); echo ($expected === $result) ? "Success\n" : "Failure: {$expected} !== {$result}\n";}
Output for 5.4.0 - 5.4.32, 7.2.29 - 7.2.33, 7.3.16 - 7.3.31, 7.4.3 - 7.4.32, 8.0.0 - 8.0.12, 8.0.14 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
<?php /** * Remove dot segments from a URI path according to RFC3986 Section 5.2.4 * * @param $path * @return string * @link http://www.ietf.org/rfc/rfc3986.txt */function removeDotPathSegments($path) { if (strpos($path, '.') === false) { return $path; }  $inputBuffer = $path; $outputStack = [];  /** * 2. While the input buffer is not empty, loop as follows: */ while ($inputBuffer != '') { /** * A. If the input buffer begins with a prefix of "../" or "./", * then remove that prefix from the input buffer; otherwise, */ if (strpos($inputBuffer, "./") === 0) { $inputBuffer = substr($inputBuffer, 2); continue; } if (strpos($inputBuffer, "../") === 0) { $inputBuffer = substr($inputBuffer, 3); continue; }  /** * B. if the input buffer begins with a prefix of "/./" or "/.", * where "." is a complete path segment, then replace that * prefix with "/" in the input buffer; otherwise, */ if ($inputBuffer === "/.") { $outputStack[] = '/'; break; } if (substr($inputBuffer, 0, 3) === "/./") { $inputBuffer = substr($inputBuffer, 2); continue; }  /** * C. if the input buffer begins with a prefix of "/../" or "/..", * where ".." is a complete path segment, then replace that * prefix with "/" in the input buffer and remove the last * segment and its preceding "/" (if any) from the output * buffer; otherwise, */ if ($inputBuffer === "/..") { array_pop($outputStack); $outputStack[] = '/'; break; } if (substr($inputBuffer, 0, 4) === "/../") { array_pop($outputStack); $inputBuffer = substr($inputBuffer, 3); continue; }  /** * D. if the input buffer consists only of "." or "..", then remove * that from the input buffer; otherwise, */ if ($inputBuffer === '.' || $inputBuffer === '..') { break; }  /** * E. move the first path segment in the input buffer to the end of * the output buffer, including the initial "/" character (if * any) and any subsequent characters up to, but not including, * the next "/" character or the end of the input buffer. */ if (($slashPos = stripos($inputBuffer, '/', 1)) === false) { $outputStack[] = $inputBuffer; break; } else { $outputStack[] = substr($inputBuffer, 0, $slashPos); $inputBuffer = substr($inputBuffer, $slashPos); } }  return implode($outputStack);}  // --- Test --- $expectations = [ ['bar', '....//bar'], ['bar', './bar'], ['bar', '.././bar'], ['bar', '.././bar'], ['/foo/bar', '/foo/./bar'], ['/bar/', '/bar/./'], ['/', '/.'], ['/bar/', '/bar/.'], ['/bar', '/foo/../bar'], ['/', '/bar/../'], ['/', '/..'], ['/', '/bar/..'], ['/foo/', '/foo/bar/..'], ['', '.'], ['', '..'],];  foreach ($expectations as $struct) { list($expected, $input) = $struct; $result = removeDotPathSegments($input); echo ($expected === $result) ? "Success\n" : "Failure: {$expected} !== {$result}\n";}
Output for 8.0.13
Parse error: syntax error, unexpected token "function" in /in/6nHT6 on line 1
Process exited with code 255.
Output for 5.4.34, 7.3.32 - 7.3.33, 7.4.33
Parse error: syntax error, unexpected 'function' (T_FUNCTION) in /in/6nHT6 on line 1
Process exited with code 255.

preferences:
114.1 ms | 402 KiB | 173 Q