Skip to content

Instantly share code, notes, and snippets.

@mneuhaus
Created January 8, 2014 16:01
Show Gist options
  • Save mneuhaus/8319106 to your computer and use it in GitHub Desktop.
Save mneuhaus/8319106 to your computer and use it in GitHub Desktop.
Little function to safely concatenate some paths easily
<?php
function path() {
$paths = func_get_args();
$prefix = '';
$suffix = '';
if (substr(current($paths), 0, 1) == DIRECTORY_SEPARATOR) {
$prefix = DIRECTORY_SEPARATOR;
}
if (substr(end($paths), -1, 1) == DIRECTORY_SEPARATOR) {
$suffix = DIRECTORY_SEPARATOR;
}
foreach ($paths as $key => $path) {
$paths[$key] = trim($path, DIRECTORY_SEPARATOR);
}
$path = $prefix . implode(DIRECTORY_SEPARATOR, $paths) . $suffix;
return $path;
}
echo path('/some', 'path', 'somewhere/') . chr(10);
// => /some/path/somewhere/
echo path('/some', '/path/', 'somewhere') . chr(10);
// => /some/path/somewhere
echo path('some', '/path/', '/somewhere/') . chr(10);
// => some/path/somewhere/
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment