str_replace有时候会重复替换,导致错误结果,比如字符串:hello play games,it is a good game 现在要替换game 替换成thing,games替换成word
$string = 'hello play games,it is a good game';
$find = array('game', 'games');
$replace = array('thing', 'word');
$result = str_replace($find, $replace, $string);
echo $result; // 输出 hello play things,it is a good thing
注意,这里的games没有被检索到,因为已经被game先替换成thing。解决方法krsort($find),先替换games,再替换game