addDirSep() can check for arbitrary chars at end of string

This commit is contained in:
Uwe Steinmann 2023-02-17 16:10:59 +01:00
parent bea6ab35a8
commit 4e08744631

View File

@ -663,16 +663,19 @@ function get_extension($mimetype) { /* {{{ */
} /* }}} */ } /* }}} */
/** /**
* Adds a missing front slash to a string * Adds a missing directory separator (or any other char) to a string
* *
* This function is used for making sure a directory name has a * This function is used for making sure a directory name has a
* trailing directory separator * trailing directory separator. It can also be used to add
* any other char at the end of a string, e.g. an URL which must
* end in '/' (DIRECTORY_SEPARATOR wouldn't be right in that case,
* because it is '\' on Windows)
*/ */
function addDirSep($str) { /* {{{ */ function addDirSep($str, $chr=DIRECTORY_SEPARATOR) { /* {{{ */
if(trim($str) == '') if(trim($str) == '')
return ''; return '';
if(substr(trim($str), -1, 1) != DIRECTORY_SEPARATOR) if(substr(trim($str), -1, 1) != $chr)
return trim($str).DIRECTORY_SEPARATOR; return trim($str).$chr;
else else
return trim($str); return trim($str);
} /* }}} */ } /* }}} */