从字符串 PHP 中删除尾部斜杠
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4319105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove Trailing Slash From String PHP
提问by Zac Brown
Is it possible to remove the trailing slash /
from a string using PHP?
是否可以/
使用 PHP 从字符串中删除尾部斜杠?
回答by ThiefMaster
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 alltrailing slashes:
另一个(可能更好)选项将使用rtrim()
- 这个删除所有尾部斜杠:
$string = rtrim($string, '/');
回答by Ross
This removes trailing slashes:
这将删除尾部斜杠:
$str = rtrim($str, '/');
回答by Dan Lugg
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 *nixto Windows, as I assume this question pertains to dealing with paths.
它确保从*nix到Windows 的可移植性,因为我认为这个问题与处理路径有关。