Skip to content

Instantly share code, notes, and snippets.

@joerx
Created January 5, 2015 06:56
Show Gist options
  • Save joerx/4e8cf93d59f812593166 to your computer and use it in GitHub Desktop.
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.
<?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