Skip to content

Instantly share code, notes, and snippets.

@AramZS
Last active August 29, 2015 14:05
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 AramZS/517e3fb404863942e70d to your computer and use it in GitHub Desktop.
Save AramZS/517e3fb404863942e70d to your computer and use it in GitHub Desktop.
PHP Path Builder
<?php
/**
* Build file paths.
*
* Build paths with arrays Call out of static function class_name->build_path
* or self::build_path. Use like:
*
* build_path(array("home", "alice", "Documents", "example.txt"));
*
* @see http://php.net/manual/en/dir.constants.php
* @global string DIRECTORY_SEPARATOR Called from class definition, system variable
*
* @param array $segments The pieces of the URL, should be array of strings. Default null Accepts string.
* @param bool $leading Optional If the returned path should have a leading slash. Default true.
* @param bool $url Optional If the returned path should use web URL style pathing or system style. Default false
* @return string The composed path.
*
*/
public function build_path($segments=array(), $leading = true, $url = false) {
if ($url){
$slash = '/';
} else {
$slash = DIRECTORY_SEPARATOR;
}
$string = join($slash, $segments);
if ($leading){
$string = $slash . $string;
}
return $string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment