如果路径字符串中的最后一个字符是斜杠,则删除它

发布于 2024-10-07 23:42:35 字数 388 浏览 0 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

不甘平庸 2024-10-14 23:42:36

安全删除最后一个 / 的一个很好的解决方案是使用

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

当存在一个或多个时,rtrim() 会删除字符串右侧的所有 /

您还可以安全地在 URL 末尾添加一个 /

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

A nice solution to remove safely the last / is to use

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

rtrim() 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:

$string = rtrim($string, '/').'/';
小矜持 2024-10-14 23:42:36

您可以使用 substr

substr($str, -1)

这将返回单字节字符串中的最后一个字节/字符。另请参阅多字节字符串变体 mb_substr

但如果您只想删除任何尾部斜杠,rtrim 可能是最好的解决方案。

由于您正在使用 URL,因此您还可以查看 parse_url 将 URL 解析为尾随斜杠不需要是 URL 路径的一部分。

You can use substr:

substr($str, -1)

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.

居里长安 2024-10-14 23:42:36

$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.

忘年祭陌 2024-10-14 23:42:36

您可以使用 basename()

这将返回字符 为 http://domainx.com/characters/ 以及 http://domainx.com/characters

您可以这样做:-

$page = $_SERVER['REQUEST_URI'];
$module = basename($page);

然后您可以使用直接在条件逻辑中使用 $module ,而不进行任何重定向。

如果您想收集最后的 / 修剪 URL,那么您可以执行以下操作:-

如果您将项目基本 url 存储在配置文件中:-

BASE_URL  = 'http://example.com'

那么您可以执行以下操作:-

$page = $_SERVER['REQUEST_URI'];
$module = basename($page);
$trimmedUrl = BASE_URL.'/'.$module;

You can use basename()

This will return characters for http://domainx.com/characters/ as well as http://domainx.com/characters

You can do like this:-

$page = $_SERVER['REQUEST_URI'];
$module = basename($page);

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:-

BASE_URL  = 'http://example.com'

then you can do this:-

$page = $_SERVER['REQUEST_URI'];
$module = basename($page);
$trimmedUrl = BASE_URL.'/'.$module;
抱着落日 2024-10-14 23:42:36

您可以在主题末尾使用 preg_replace() 一个 /

$url = 'http://domainx.com/characters/';
$url = preg_replace('/(?:\/)$/', '', $url);

You could preg_replace() a / at the end of the subject

$url = 'http://domainx.com/characters/';
$url = preg_replace('/(?:\/)$/', '', $url);
怕倦 2024-10-14 23:42:36

如果你有 php > 7.1

$string[-1]

会给你最后一个字符

http://sandbox.onlinephpfunctions.com/code/ff439889f14906749e4eb6328796 c354c60f269b

rtrim 和自定义函数之间的区别:

<?php

$string0 = 'hi//';
$string1 = 'hello/';
$string2 = 'world';

function untrailingslashit( $string ) {
    return $string[-1] === '/' ? substr( $string, 0, -1) : $string;
}

echo untrailingslashit($string0);
echo "\n";
echo untrailingslashit($string1);
echo "\n";
echo untrailingslashit($string2);
echo "\n";
echo rtrim($string0, "/");

结果:

hi/
hello
world
hi

If you have php > 7.1

$string[-1]

Will give you the last character

http://sandbox.onlinephpfunctions.com/code/ff439889f14906749e4eb6328796c354c60f269b

Difference between rtrim and custom function:

<?php

$string0 = 'hi//';
$string1 = 'hello/';
$string2 = 'world';

function untrailingslashit( $string ) {
    return $string[-1] === '/' ? substr( $string, 0, -1) : $string;
}

echo untrailingslashit($string0);
echo "\n";
echo untrailingslashit($string1);
echo "\n";
echo untrailingslashit($string2);
echo "\n";
echo rtrim($string0, "/");

Result:

hi/
hello
world
hi
水波映月 2024-10-14 23:42:36

在 PHP 8 中,

str_ends_with($string, '/');

新的 str_starts_with()str_ends_with() 函数被添加到核心中。

With PHP 8

str_ends_with($string, '/');

New str_starts_with() and str_ends_with() functions are added into the core.

南街九尾狐 2024-10-14 23:42:36

这直接来自 WordPress:

function untrailingslashit( $string ) {
    return rtrim( $string, '/\\' );
}

This is coming straight from WordPress:

function untrailingslashit( $string ) {
    return rtrim( $string, '/\\' );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文