同时替换所有单词
Replacing all words at the same time
我正在尝试使用 str_replace() 同时替换所有单词,但我不确定该怎么做。基本上,我需要同时将 you
更改为 me
并将 me
更改为 you
。我该怎么做?
<?php
$string = "you me";
$string = str_replace("you", "me", $string);
$string = str_replace("me", "you", $string);
echo $string;
?>
结果:
you you
需要结果:
me you
strtr() 可以接受对数组 'from' => 'to'
替换为第二个参数:
echo strtr($string, array('you' => 'me', 'me' => 'you'));
我正在尝试使用 str_replace() 同时替换所有单词,但我不确定该怎么做。基本上,我需要同时将 you
更改为 me
并将 me
更改为 you
。我该怎么做?
<?php
$string = "you me";
$string = str_replace("you", "me", $string);
$string = str_replace("me", "you", $string);
echo $string;
?>
结果:
you you
需要结果:
me you
strtr() 可以接受对数组 'from' => 'to'
替换为第二个参数:
echo strtr($string, array('you' => 'me', 'me' => 'you'));