Created
January 5, 2015 06:56
-
-
Save joerx/4e8cf93d59f812593166 to your computer and use it in GitHub Desktop.
PHP: Simple way to strip slashes and assemble a path-like structure avoiding missing/duplicate slashes.
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 | |
function mkUri($base, $path) { | |
$base = preg_replace("/\/$/", "", $base); | |
$path = preg_replace("/^\//", "", $path); | |
return "$base/$path"; | |
} | |
echo mkUri("http://localhost/", "/foo") . "\n"; | |
echo mkUri("http://localhost/", "foo") . "\n"; | |
echo mkUri("http://localhost", "/foo") . "\n"; | |
echo mkUri("http://localhost", "foo") . "\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment