对于字符串替换,我们最常用的函数是str_replace。但如果我们的字符串是如下:
- hi all, I said hello
现在要将hi换成hello,再将hello换成hi,用str_replace处理起来就不方便了
这时,用strtr函数可以很轻松的解决这个问题
- $trans = array("hello" => "hi", "hi" => "hello");
- $subject = "hi all, I said hello";
- echo strtr($subject, $trans);
输出:
- hello all, I said hi