PHP. Remove Last Directory Separator From Path

This PHP snippet is used to remove the last directory separator from a given path, if it ends with directory separator character.

if (substr(trim($path), -1) == DIRECTORY_SEPARATOR) {
    $path = substr(trim($path), 0, -1);
}