php PHP替换字符串中最后一次出现的字符串?

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

PHP Replace last occurrence of a String in a String?

phpstring

提问by Kirk Ouimet

Anyone know of a very fast way to replace the last occurrence of a string with another string in a string?

有人知道用字符串中的另一个字符串替换最后一次出现的字符串的非常快速的方法吗?

回答by Mischa

You can use this function:

您可以使用此功能:

function str_lreplace($search, $replace, $subject)
{
    $pos = strrpos($subject, $search);

    if($pos !== false)
    {
        $subject = substr_replace($subject, $replace, $pos, strlen($search));
    }

    return $subject;
}

回答by ricka

Another 1-liner but without preg:

另一个 1-liner 但没有 preg:

$subject = 'bourbon, scotch, beer';
$search = ',';
$replace = ', and';

echo strrev(implode(strrev($replace), explode(strrev($search), strrev($subject), 2))); //output: bourbon, scotch, and beer

回答by zack

$string = 'this is my world, not my world';
$find = 'world';
$replace = 'farm';
$result = preg_replace(strrev("/$find/"),strrev($replace),strrev($string),1);
echo strrev($result); //output: this is my world, not my farm

回答by John Sonderson

The following rather compact solution uses the PCRE positive lookahead assertionto match the last occurrence of the substring of interest, that is, an occurrence of the substring which is not followed by any other occurrences of the same substring. Thus the example replaces the last 'fox'with 'dog'.

以下相当紧凑的解决方案使用PCRE 正向先行断言来匹配感兴趣的子串的最后一次出现,即,该子串的出现后没有任何其他出现的相同子串。因此,例如将替换last 'fox''dog'

$string = 'The quick brown fox, fox, fox jumps over the lazy fox!!!';
echo preg_replace('/(fox(?!.*fox))/', 'dog', $string);

OUTPUT: 

输出: 

The quick brown fox, fox, fox jumps over the lazy dog!!!

回答by Nicolas Finelli

You could do this:

你可以这样做:

$str = 'Hello world';
$str = rtrim($str, 'world') . 'John';

Result is 'Hello John';

结果是“你好约翰”;

Regards

问候

回答by alexis

This is an ancient question, but why is everyone overlooking the simplest regexp-based solution? Normal regexp quantifiers are greedy, people! If you want to find the last instance of a pattern, just stick .*in front of it. Here's how:

这是一个古老的问题,但为什么每个人都忽略了最简单的基于正则表达式的解决方案?正常的正则表达式量词是贪婪的,伙计们!如果你想找到一个模式的最后一个实例,只需坚持.*在它前面。就是这样:

$text = "The quick brown fox, fox, fox, fox, jumps over etc.";
$fixed = preg_replace("((.*)fox)", "DUCK", $text);
print($fixed);

This will replace the lastinstance of "fox" to "DUCK", like it's supposed to, and print:

这会将“fox”的最后一个实例替换为“DUCK”,就像它应该的那样,并打印:

The quick brown fox, fox, fox, DUCK, jumps over etc.

回答by Frank

Just one line of code (late answer but it's worth to add it):

只需一行代码(答案较晚,但值得添加):

$string = 'The quick brown fox jumps over the lazy dog';
$find_me = 'dog';

preg_replace('/'. $find_me .'$/', '', $string);

the ending $ indicates the end of the string.

结尾的 $ 表示字符串的结尾。

回答by Alix Axel

This will also work:

这也将起作用:

function str_lreplace($search, $replace, $subject)
{
    return preg_replace('~(.*)' . preg_quote($search, '~') . '(.*?)~', '' . $replace . '', $subject, 1);
}

UPDATESlightly more concise version (http://ideone.com/B8i4o):

更新稍微简洁的版本(http://ideone.com/B8i4o):

function str_lreplace($search, $replace, $subject)
{
    return preg_replace('~(.*)' . preg_quote($search, '~') . '~', '' . $replace, $subject, 1);
}

回答by Faruk üNAL

You can use strrpos() to find last match.

您可以使用 strrpos() 查找最后一个匹配项。

$string = "picture_0007_value";
$findChar =strrpos($string,"_");

$string[$findChar]=".";

echo $string;

Output : picture_0007.value

输出:图片_0007.value

回答by xwero

$string = "picture_0007_value";
$findChar =strrpos($string,"_");
if($findChar !== FALSE) {
  $string[$findChar]=".";
}

echo $string;

Apart from the mistakes in the code, Faruk Unal has the best anwser. One function does the trick.

除了代码中的错误,Faruk Unal 拥有最好的答案。一个函数可以解决问题。