我试图将字符串中的2个单词相互替换,同时忽略它们的情况,但我似乎无法找出使其正常工作的方法。请看看我的代码:
$text = "you and me";
$text = str_ireplace("you","me",$text);
//$text is "me and me";
$text = str_ireplace("me","you",$text);
//$text is "you and you";
预期结果: "me and you";
实际结果:"you and you";
编辑:这个实际不包括忽略大小写。
这很简单,只需使用str_ireplace:
$text = "YoU and mE";
$from = array('you', 'me', '__TMP__');
$to = array('__TMP__', 'you', 'me');
$text = str_ireplace($from, $to, $text);
检查:http://php.net/manual/en/function.strtr.php
$trans = array("you" => "me", "me" => "you");
echo strtr("you and me", $trans);