简体   繁体   English

从字符串PHP中删除尾随斜杠

[英]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: 另一个(可能更好)选项是使用rtrim() - 这个删除所有尾部斜杠:

$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: rtrim()很棒,但实现方式如下:

$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. 它确保从* nixWindows的可移植性,因为我认为这个问题与处理路径有关。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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