简体   繁体   中英

Remove Trailing Slash From String PHP

是否可以使用PHP从字符串中删除尾部斜杠/

Sure it is, simply check if the last character is a slash and then nuke that one.

if(substr($string, -1) == '/') {
    $string = substr($string, 0, -1);
}

Another (probably better) option would be using rtrim() - this one removes all trailing slashes:

$string = rtrim($string, '/');

这会删除尾部斜杠:

$str = rtrim($str, '/');

Long accepted, however in my related searches I stumbled here, and am adding for "completeness"; rtrim() is great, however implemented like this:

$string = rtrim($string, '/\\'); //strip both forward and back slashes

It ensures portability from *nix to Windows , as I assume this question pertains to dealing with paths.

rtrim使用rtrim导致它尊重字符串并不以尾部斜杠结束

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM