$text = "abc def ghi abc def ghi abc def ghi abc"
$search = "abc";
$regex = '/(\s)'.$search.'(\s)/i';
$array_key = array();
if(preg_match_all($regex, $text, $tmp)) {
$array_key = $tmp[0];
$n = count($tmp[0]);
for($i=0; $i<$n; $i++) {
if($n % 2 == 0) {
$content = str_replace($array_key[$i], 'ABC', $text);
}
}
当我 echo $content 输出时:
" ABC def ghi ABC def ghi ABC def ghi ABC
"
但我想要的结果是“ ABC def ghi abc def ghi ABC def ghi abc
”,因为$n % 2 == 0
,如何解决?