Skip to content

Instantly share code, notes, and snippets.

@stemar
Created December 7, 2019 00:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stemar/bd4dceff1cea916517f7aaefda6861e3 to your computer and use it in GitHub Desktop.
Save stemar/bd4dceff1cea916517f7aaefda6861e3 to your computer and use it in GitHub Desktop.
Normalize a path in PHP
<?php
/**
* Normalize a path.
*
* Usage: path('./one/', '/two/', 'three/');
* Result: "./one/two/three"
* @param array $parts
* @return string
*/
function path(...$parts) {
$parts = array_filter($parts);
$first = rtrim(array_shift($parts), DIRECTORY_SEPARATOR);
$parts = array_map(function ($value) {
return trim($value, DIRECTORY_SEPARATOR);
}, $parts);
array_unshift($parts, $first);
return join(DIRECTORY_SEPARATOR, $parts);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment