php 删除字符串的一部分,但仅当它位于字符串的末尾时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5573334/
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 a part of a string, but only when it is at the end of the string
提问by Dylan
I need to remove a substring of a string, but only when it is at the END of the string.
我需要删除字符串的子字符串,但仅当它位于字符串的末尾时。
for example, removing 'string' at the end of the following strings :
例如,删除以下字符串末尾的“字符串”:
"this is a test string" -> "this is a test "
"this string is a test string" - > "this string is a test "
"this string is a test" -> "this string is a test"
Any idea's ? Probably some kind of preg_replace, but how??
有任何想法吗 ?可能是某种 preg_replace,但如何?
回答by Tim Cooper
You'll note the use of the $
character, which denotes the end of a string:
您会注意到$
字符的使用,它表示字符串的结尾:
$new_str = preg_replace('/string$/', '', $str);
If the string is a user supplied variable, it is a good idea to run it through preg_quote
first:
如果字符串是用户提供的变量,最好先运行它preg_quote
:
$remove = $_GET['remove']; // or whatever the case may be
$new_str = preg_replace('/'. preg_quote($remove, '/') . '$/', '', $str);
回答by Skrol29
Using regexp may fails if the substring has special characters.
如果子字符串具有特殊字符,则使用正则表达式可能会失败。
The following will work with any strings:
以下将适用于任何字符串:
$substring = 'string';
$str = "this string is a test string";
if (substr($str,-strlen($substring))===$substring) $str = substr($str, 0, strlen($str)-strlen($substring));
回答by Bas
I wrote these two function for left and right trim of a string:
我为字符串的左右修剪编写了这两个函数:
/**
* @param string $str Original string
* @param string $needle String to trim from the end of $str
* @param bool|true $caseSensitive Perform case sensitive matching, defaults to true
* @return string Trimmed string
*/
function rightTrim($str, $needle, $caseSensitive = true)
{
$strPosFunction = $caseSensitive ? "strpos" : "stripos";
if ($strPosFunction($str, $needle, strlen($str) - strlen($needle)) !== false) {
$str = substr($str, 0, -strlen($needle));
}
return $str;
}
/**
* @param string $str Original string
* @param string $needle String to trim from the beginning of $str
* @param bool|true $caseSensitive Perform case sensitive matching, defaults to true
* @return string Trimmed string
*/
function leftTrim($str, $needle, $caseSensitive = true)
{
$strPosFunction = $caseSensitive ? "strpos" : "stripos";
if ($strPosFunction($str, $needle) === 0) {
$str = substr($str, strlen($needle));
}
return $str;
}
回答by Pascal MARTIN
I suppose you could use a regular expression, which would match string
and, then, end of string, coupled with the preg_replace()
function.
我想你可以使用正则表达式,它会匹配string
,然后是字符串的结尾,再加上preg_replace()
函数。
Something like this should work just fine :
这样的事情应该可以正常工作:
$str = "this is a test string";
$new_str = preg_replace('/string$/', '', $str);
Notes :
注意事项:
string
matches... well...string
- and
$
means end of string
string
比赛……嗯……string
- 并
$
表示字符串的结尾
For more informations, you can read the Pattern Syntaxsection of the PHP manual.
有关更多信息,您可以阅读PHP 手册的模式语法部分。
回答by Hasan Tayyar BESIK
preg_replace and this pattern : /string\z/i
preg_replace 和这个模式:/string\z/i
\z means end of the string
\z 表示字符串结束
回答by Marco Panichi
IF you don't mind about performance AND the part of the string could be placed only at the end of string, THEN you can do this:
如果您不介意性能并且字符串的一部分只能放在字符串的末尾,那么您可以这样做:
$string = "this is a test string";
$part = "string";
$string = implode( $part, array_slice( explode( $part, $string ), 0, -1 ) );
echo $string;
// OUTPUT: "this is a test "