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

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

Remove a part of a string, but only when it is at the end of the string

phpstringreplacepreg-replace

提问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_quotefirst:

如果字符串是用户提供的变量,最好先运行它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 stringand, 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 :


注意事项:

  • stringmatches... 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 表示字符串结束

http://tr.php.net/preg_replace

http://tr.php.net/preg_replace

回答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 "

回答by umpirsky

You can use rtrim().

您可以使用rtrim()

php > echo rtrim('this is a test string', 'string');
this is a test 

This will work only in some cases, as 'string'is just a characters mask and char order will not be respected.

这仅在某些情况下有效,因为'string'它只是一个字符掩码,字符顺序不会得到遵守。