Be careful when replacing characters (or repeated patterns in the FROM and TO arrays): For example: I would expect as result: "ZDDB" However, this return: "ZDDD" (Because B = D according to our array) To make this work, use "strtr" instead: "A","2" => "B","3" => "C","B" => "D"); $word = "ZBB2"; echo strtr($word,$arr); ?> This returns: "ZDDB"
一、在PHP中替换有三种方式
1、strstr
2、str_replace
3、preg_match
都可满足你的需要
二、效率
效率上 str_replace > strtr > preg_match
三、str_replace与strtr的一些不同
四、结论
所以一般用str_replace , 需要正匹配的时候用preg_match
http://php.net/manual/en/function.str-replace.php
http://php.net/manual/zh/function.strtr.php
http://www.w3school.com.cn/php/func_string_str_replace.asp
用str_replace函数传入数组即可
PHP自带的字符串替换函数就完全满足你的要求了 建议有需求前去看下官方的手册 很多功能都有函数支持了