據(jù)說比 str_replace 快
/**
* [strtr 函數(shù)的用法]
*
* 用法1: string strtr ( string $str , string $from , string $to )
* 如果 from 與 to 長度不相等,那么多余的字符部分將被忽略。 str 的長度將會和返回的值一樣芦圾。
*
* 用法2: string strtr ( string $str , array $replace_pairs )
*/
$str = 'http://www.baidu.com/?name=zhangsan&type=http';
/**
* 輸出: accd://www.baidu.com/?name=zaangsan&cyde=accd
* 替換: ['h'=>'a', 't'=>'b', 't'=>'c', 'p'=>'d'], e 超長忽略
*/
$_str = strtr($str, 'http', 'abcde');
echo $_str . PHP_EOL;
/**
* 輸出: https://www.doubi.com/?name=張三&type=https
* 這種場景可能用的比較多
*/
$_str = strtr($str, ['http' => 'https', 'baidu' => 'doubi', 'zhangsan' => '張三']);
echo $_str . PHP_EOL;