如果路径字符串中的最后一个字符是斜杠,则删除它
我正在 PHP 中进行一些 url 重写,需要找到末尾带有斜杠的 URL,然后执行 301 重定向。我以为会有一个简单的 PHP 函数来查找最后一个字符串,但我找不到任何东西。第一直觉让我认为我需要使用正则表达式,但我并不是 100%。
这是一个示例:
http://domainx.com/characters/ 我想找到一个尾随斜杠并将其旋转进入 http://domainx.com/characters
那么什么函数可以帮助我检查最后一个字符是否是“/ “?
I'm doing some url rewriting in PHP and need to find URLS with a slash at the end and then do a 301 redirect. I thought there'd be a simple PHP function to find last string, but I couldn't find anything. First instincts make m think I need to use regex, but I'm not 100%.
Here's one example:
http://domainx.com/characters/ I want to find a trailing slash and turn it into http://domainx.com/characters
So what function will help me check if the last character is a "/"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
安全删除最后一个
/
的一个很好的解决方案是使用当存在一个或多个时,
rtrim()
会删除字符串右侧的所有/
。您还可以安全地在 URL 末尾添加一个
/
:A nice solution to remove safely the last
/
is to usertrim()
removes all/
s on the right side of the string when there is one or more.You can also safely add exactly one single
/
at the end of an URL:您可以使用
substr
:这将返回单字节字符串中的最后一个字节/字符。另请参阅多字节字符串变体
mb_substr
。但如果您只想删除任何尾部斜杠,
rtrim
可能是最好的解决方案。由于您正在使用 URL,因此您还可以查看
parse_url
将 URL 解析为尾随斜杠不需要是 URL 路径的一部分。You can use
substr
:This returns the last byte/character in a single-byte string. See also the multi-byte string variant
mb_substr
.But if you just want to remove any trailing slashes,
rtrim
is probably the best solution.And since you’re working with URLs, you might also take a look at
parse_url
to parse URLs as a trailing slash does not need to be part of the URL path.$string[strlen($string)-1]
给出最后一个字符。但如果你想去掉尾部斜杠,你可以这样做
$string = rtrim($string, '/');
。如果没有尾部斜杠,$string
将保持不变。$string[strlen($string)-1]
gives you the last character.But if you want to strip trailing slashes, you can do
$string = rtrim($string, '/');
. If there is no trailing slash,$string
will remain unchanged.您可以使用 basename()
这将返回
字符 为
http://domainx.com/characters/
以及http://domainx.com/characters
您可以这样做:-
然后您可以使用直接在条件逻辑中使用
$module
,而不进行任何重定向。如果您想收集最后的
/
修剪 URL,那么您可以执行以下操作:-如果您将项目基本 url 存储在配置文件中:-
那么您可以执行以下操作:-
You can use basename()
This will return
characters
forhttp://domainx.com/characters/
as well ashttp://domainx.com/characters
You can do like this:-
Then you can use the
$module
directly in your conditional logic without doing any redirects.If you want to collect the last
/
trimmed URL then you can do this:-If you are storing the project base url in a config file:-
then you can do this:-
您可以在主题末尾使用
preg_replace()
一个/
You could
preg_replace()
a/
at the end of the subject如果你有 php > 7.1
会给你最后一个字符
http://sandbox.onlinephpfunctions.com/code/ff439889f14906749e4eb6328796 c354c60f269b
rtrim 和自定义函数之间的区别:
结果:
If you have php > 7.1
Will give you the last character
http://sandbox.onlinephpfunctions.com/code/ff439889f14906749e4eb6328796c354c60f269b
Difference between rtrim and custom function:
Result:
在 PHP 8 中,
新的
str_starts_with()
和str_ends_with()
函数被添加到核心中。With PHP 8
New
str_starts_with()
andstr_ends_with()
functions are added into the core.这直接来自 WordPress:
This is coming straight from WordPress: