在忽略大小写的情况下相互替换 2 个单词,PHP
Replacing 2 words with each other while ignore case, PHP
我正在尝试将一个字符串中的 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);
我正在尝试将一个字符串中的 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);