Created
November 13, 2020 12:42
-
-
Save anwinged/5c599fad83956f031205bf1d823c299e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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