文字列の最後の出現を文字列内の別の文字列に置き換える非常に高速な方法を知っている人はいますか?
文字列の最後の出現は、文字列の最後の文字ではない可能性があることに注意してください。
例:
$search = 'The';
$replace = 'A';
$subject = 'The Quick Brown Fox Jumps Over The Lazy Dog';
期待される出力:
The Quick Brown Fox Jumps Over A Lazy Dog
文字列の最後の出現を文字列内の別の文字列に置き換える非常に高速な方法を知っている人はいますか?
文字列の最後の出現は、文字列の最後の文字ではない可能性があることに注意してください。
例:
$search = 'The';
$replace = 'A';
$subject = 'The Quick Brown Fox Jumps Over The Lazy Dog';
期待される出力:
The Quick Brown Fox Jumps Over A Lazy Dog
この機能を使用できます:
function str_lreplace($search, $replace, $subject)
{
$pos = strrpos($subject, $search);
if($pos !== false)
{
$subject = substr_replace($subject, $replace, $pos, strlen($search));
}
return $subject;
}
別の 1 ライナーですが、preg はありません。
$subject = 'bourbon, scotch, beer';
$search = ',';
$replace = ', and';
echo strrev(implode(strrev($replace), explode(strrev($search), strrev($subject), 2))); //output: bourbon, scotch, and beer
$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
次のかなりコンパクトな解決策は、PCRE 肯定先読みアサーションを使用して、対象の部分文字列の最後の出現、つまり、同じ部分文字列の他の出現が後に続かない部分文字列の出現と一致させます。last 'fox'
したがって、この例ではをに置き換えます'dog'
。
$string = 'The quick brown fox, fox, fox jumps over the lazy fox!!!';
echo preg_replace('/(fox(?!.*fox))/', 'dog', $string);
出力:
The quick brown fox, fox, fox jumps over the lazy dog!!!
これも機能します:
function str_lreplace($search, $replace, $subject)
{
return preg_replace('~(.*)' . preg_quote($search, '~') . '(.*?)~', '$1' . $replace . '$2', $subject, 1);
}
更新少し簡潔なバージョン ( http://ideone.com/B8i4o ):
function str_lreplace($search, $replace, $subject)
{
return preg_replace('~(.*)' . preg_quote($search, '~') . '~', '$1' . $replace, $subject, 1);
}
これは古くからの質問ですが、なぜ誰もが最も単純な正規表現ベースのソリューションを見落としているのでしょうか? 通常の正規表現量指定子は貪欲です、人々! パターンの最後のインスタンスを見つけたい場合は、その.*
前に固執してください。方法は次のとおりです。
$text = "The quick brown fox, fox, fox, fox, jumps over etc.";
$fixed = preg_replace("((.*)fox)", "$1DUCK", $text);
print($fixed);
これにより、「fox」の最後のインスタンスが「DUCK」に置き換えられ、想定どおりに出力されます。
The quick brown fox, fox, fox, DUCK, jumps over etc.
strrpos() を使用して、最後の一致を見つけることができます。
$string = "picture_0007_value";
$findChar =strrpos($string,"_");
$string[$findChar]=".";
echo $string;
出力:picture_0007.value
受け入れられた回答の省略形
function str_lreplace($search, $replace, $subject){
return is_numeric($pos=strrpos($subject,$search))?
substr_replace($subject,$replace,$pos,strlen($search)):$subject;
}
以下の修正されたザックのコード ( https://stackoverflow.com/a/11144999/9996503 )。正規表現に注意!このことを考慮:
$string = 'Round brackets (parentheses) "()", square brackets "()"';
$find = '()';
$replace = '[]';
// Zack's code:
$result = preg_replace(strrev("/$find/"),strrev($replace),strrev($string),1);
var_dump($result); // Output: NULL
// Correct code:
$result = strrev(preg_replace('/'.preg_quote(strrev($find)).'/', strrev($replace), strrev($string), 1));
echo $result; //Output: Round brackets (parentheses) "()", square brackets "[]"