php strtr 函数与str_replace性能比较
发布时间:2022-06-20 08:43:09 所属栏目:PHP教程 来源:互联网
导读:PHP 的 strtr 函数,性能要比 str_replace 函数高,可以代替 str_replace 来使用. strtr 有两种形式: string strtr ( string $str , string $from , string $to) string strtr ( string $str , array $replace_pairs ) 当使用第一种的时候, 参数 $from, $to 的
|
PHP 的 strtr 函数,性能要比 str_replace 函数高,可以代替 str_replace 来使用. strtr 有两种形式: string strtr ( string $str , string $from , string $to) string strtr ( string $str , array $replace_pairs ) 当使用第一种的时候, 参数 $from, $to 的字符串长度一定要相同, 否则多余的(不管是$from多还是$to多) 字符被忽略.比如 $str = 'a-=b' ; 当$from='-=' ,$to='CD',输出'aCDb', 因为'-='与'CD'的长度相同,没有问题. 当$from='-=' ,$to='CDE',输出'aCDb', $to里的'E'被忽略. 当$from='-=' ,$to='C',输出'aC=b', $from里的'='被忽略. 而使用第二种形式, 则没有这个问题, 多余的字条不会忽略. 所以,如果故意用 strtr 函数代替 str_replace, 并且使用了第一种形式, 则一定要注意这个特征, 这可能是一个陷阱. (编辑:盐城站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
站长推荐
热点阅读



