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...
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/'

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

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