Skip to content

Instantly share code, notes, and snippets.

@anwinged
Created November 13, 2020 12:42
Show Gist options
  • Save anwinged/5c599fad83956f031205bf1d823c299e to your computer and use it in GitHub Desktop.
Save anwinged/5c599fad83956f031205bf1d823c299e to your computer and use it in GitHub Desktop.
<?php
/**
* @param string ...$rest
*/
function join_path(...$rest): string
{
if (count($rest) === 0) {
return '';
}
if (count($rest) === 1) {
return $rest[0];
}
$head = array_shift($rest);
$head = $head === '/' ? $head : rtrim($head, '/');
$tail = join_path(...$rest);
return $tail ? rtrim($head, '/') . '/' . ltrim($tail, '/') : $head;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment