Last active
August 29, 2015 14:05
-
-
Save AramZS/517e3fb404863942e70d to your computer and use it in GitHub Desktop.
PHP Path Builder
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 | |
/** | |
* 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