我想执行str_replace(),但仅在第N次出现时执行。输入:
str_replace()
$originalString = "Hello world, what do you think of today's weather"; $findString = ' '; $nthOccurrence = 8; $newWord = ' beautiful ';
预期输出:
Hello world, what do you think of today's beautiful weather
bweufnob1#
下面是一个使用\K的正则表达式,它允许您替换第n次出现的字符串,而不会重复模式中的指针。如果您的搜索字符串是动态的,并且可能包含具有特殊含义的字符,则preg_quote()对于模式的完整性至关重要。如果您想静态地将搜索字符串和第n个匹配项写入您的模式,可以是:
\K
preg_quote()
(?:.*?\K ){8}
(?:[^ ]*\K ){8}
\K告诉正则表达式模式"忘记"全字符串匹配中任何先前匹配的字符。换句话说,"重新开始全字符串匹配"或"Keep from here"。在这种情况下,模式只保留第8个空格字符。代码:(Demo)
function replaceNth(string $input, string $find, string $replacement, int $nth = 1): string { $pattern = '/(?:.*?\K' . preg_quote($find, '/') . '){' . $nth . '}/'; return preg_replace($pattern, $replacement, $input, 1); } echo replaceNth($originalString, $findString, $newWord, $nthOccurrence); // Hello world, what do you think of today's beautiful weather
另一个关于如何解决这个问题的观点是:"如何在搜索字符串的第n个示例后插入新字符串?"这里有一个限制爆炸的非正则表达式方法,将新字符串添加到最后一个元素的前面,然后重新连接元素。(Demo)
$originalString = "Hello world, what do you think of today's weather"; $findString = ' '; $nthOccurrence = 8; $newWord = 'beautiful '; // notice that leading space was removed function insertAfterNth($input, $find, $newString, $nth = 1) { $parts = explode($find, $input, $nth + 1); $parts[$nth] = $newString . $parts[$nth]; return implode($find, $parts); } echo insertAfterNth($originalString, $findString, $newWord, $nthOccurrence); // Hello world, what do you think of today's beautiful weather
gudnpqoy2#
我在这里找到了答案-https://gist.github.com/VijayaSankarN/0d180a09130424f3af97b17d276b72bd
$subject = "Hello world, what do you think of today's weather"; $search = ' '; $occurrence = 8; $replace = ' nasty '; /** * String replace nth occurrence * * @param type $search Search string * @param type $replace Replace string * @param type $subject Source string * @param type $occurrence Nth occurrence * @return type Replaced string */ function str_replace_n($search, $replace, $subject, $occurrence) { $search = preg_quote($search); echo preg_replace("/^((?:(?:.*?$search){".--$occurrence."}.*?))$search/", "$1$replace", $subject); } str_replace_n($search, $replace, $subject, $occurrence);
monwx1rj3#
$originalString = "Hello world, what do you think of today's weather"; $findString = ' '; $nthOccurrence = 8; $newWord = ' beautiful '; $array = str_split($originalString); $count = 0; $num = 0; foreach ($array as $char) { if($findString == $char){ $count++; } $num++; if($count == $nthOccurrence){ array_splice( $array, $num, 0, $newWord ); break; } } $newString = ''; foreach ($array as $char) { $newString .= $char; } echo $newString;
yc0p9oo04#
我会考虑这样的事情:
function replaceNth($string, $substring, $replacement, $nth = 1){ $a = explode($substring, $string); $n = $nth-1; for($i=0,$l=count($a)-1; $i<$l; $i++){ $a[$i] .= $i === $n ? $replacement : $substring; } return join('', $a); } $originalString = 'Hello world, what do you think of today\'s weather'; $test = replaceNth($originalString, ' ', ' beautiful ' , 8); $test2 = replaceNth($originalString, 'today\'s', 'good');
cyej8jka5#
首先分解一个字符串的各个部分,然后将各个部分连接在一起,并与搜索字符串连接,但在特定的数字处与替换字符串连接(为了方便起见,这里的数字从0开始):
function str_replace_nth($search, $replace, $subject, $number = 0) { $parts = explode($search, $subject); $lastPartKey = array_key_last($parts); $result = ''; foreach($parts as $key => $part) { $result .= $part; if($key != $lastPartKey) { if($key == $number) { $result .= $replace; } else { $result .= $search; } } } return $result; }
用法:
$originalString = "Hello world, what do you think of today's weather"; $findString = ' '; $nthOccurrence = 7; $newWord = ' beautiful '; $result = str_replace_nth($findString, $newWord, $originalString, $nthOccurrence);
5条答案
按热度按时间bweufnob1#
下面是一个使用
\K
的正则表达式,它允许您替换第n次出现的字符串,而不会重复模式中的指针。如果您的搜索字符串是动态的,并且可能包含具有特殊含义的字符,则preg_quote()
对于模式的完整性至关重要。如果您想静态地将搜索字符串和第n个匹配项写入您的模式,可以是:
(?:.*?\K ){8}
(?:[^ ]*\K ){8}
\K
告诉正则表达式模式"忘记"全字符串匹配中任何先前匹配的字符。换句话说,"重新开始全字符串匹配"或"Keep from here"。在这种情况下,模式只保留第8个空格字符。代码:(Demo)
另一个关于如何解决这个问题的观点是:"如何在搜索字符串的第n个示例后插入新字符串?"这里有一个限制爆炸的非正则表达式方法,将新字符串添加到最后一个元素的前面,然后重新连接元素。(Demo)
gudnpqoy2#
我在这里找到了答案-https://gist.github.com/VijayaSankarN/0d180a09130424f3af97b17d276b72bd
monwx1rj3#
yc0p9oo04#
我会考虑这样的事情:
cyej8jka5#
首先分解一个字符串的各个部分,然后将各个部分连接在一起,并与搜索字符串连接,但在特定的数字处与替换字符串连接(为了方便起见,这里的数字从0开始):
用法: