Add ending slash in PHP

Adds an ending slash to the given path, makes a difference <br/> between windows and unix paths (keeps orginal slashes) <br/> The normalize_path function is also a good way for doing this...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function add_ending_slash($path){
 
    $slash_type = (strpos($path'\')===0) ? 'win' : 'unix'; 
 
    $last_char = substr($path, strlen($path)-1, 1);
 
    if ($last_char != '/' and $last_char != '\') {
        // no slash:
        $path .= ($slash_type == 'win') ? '\' : '/';
    }
 
    return $path;
}
//Example:
print add_ending_slash('/foo/bar/hd');
// returns '/foo/bar/hd/'
X

Url: http://www.jonasjohn.de/snippets/php/add-ending-slash.htm

Language: PHP | User: ShareMySnippets | Created: Jan 12, 2014 | Tags: ending slash