php 在PHP中查找字符串中的最后一个字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4427172/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:03:01  来源:igfitidea点击:

Find last character in a string in PHP

phpregexstringurl-rewritingcharacter

提问by Bob Cavezza

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

我正在用 PHP 进行一些 url 重写,需要找到末尾带斜杠的 URL,然后进行 301 重定向。我以为会有一个简单的 PHP 函数来查找最后一个字符串,但我找不到任何东西。第一直觉让我觉得我需要使用正则表达式,但我不是 100%。

Here's one example:

下面是一个例子:

http://domainx.com/characters/I want to find a trailing slash and turn it into http://domainx.com/characters

http://domainx.com/characters/我想找到一个尾部斜杠并将其变成http://domainx.com/characters

So what function will help me check if the last character is a "/"?

那么什么函数可以帮助我检查最后一个字符是否是“/”?

回答by powtac

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.

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

You can also safely add exactly one single /at the end of an URL:

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

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

回答by Gumbo

You can use substr:

您可以使用substr

substr($str, -1)

This returns the last byte/character in a single-byte string. See also the multi-byte string variant mb_substr.

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

But if you just want to remove any trailing slashes, rtrimis probably the best solution.

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

And since you're working with URLs, you might also take a look at parse_urlto parse URLs as a trailing slash does not need to be part of the URL path.

并且由于您正在处理 URL,因此您可能还会查看parse_url解析 URL,因为尾部斜杠不需要是 URL 路径的一部分。

回答by rik

$string[strlen($string)-1]gives you the last character.

$string[strlen($string)-1]给你最后一个字符。

But if you want to strip trailing slashes, you can do $string = rtrim($string, '/');. If there is no trailing slash, $stringwill remain unchanged.

但是如果你想去掉尾部的斜杠,你可以做$string = rtrim($string, '/');. 如果没有尾随斜线,$string将保持不变。

回答by Sandeepan Nath

You can use basename()

您可以使用basename()

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

这将返回charactershttp://domainx.com/characters/还有http://domainx.com/characters

You can do like this:-

你可以这样做:-

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

Then you can use the $moduledirectly in your conditional logic without doing any redirects.

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

If you want to collect the last /trimmed URL then you can do this:-

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

If you are storing the project base url in a config file:-

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

BASE_URL  = 'http://example.com'

then you can do this:-

那么你可以这样做:-

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

回答by Biotox

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

你可以preg_replace()一个/拍摄对象的结束

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

回答by nektobit

If you have php > 7.1

如果你有 php > 7.1

$string[-1]

Will give you the last character

会给你最后一个字符

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

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