教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php strtr 函数与str_replace性能比较

php strtr 函数与str_replace性能比较

发布时间:2019-02-05   编辑:jiaochengji.com
教程集为您提供php strtr 函数与str,replace性能比较等资源,欢迎您收藏本站,我们将为您提供最新的php strtr 函数与str,replace性能比较资源
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, 并且使用了第一种形式, 则一定要注意这个特征, 这可能是一个陷阱.

您可能感兴趣的文章:
php strtr 函数与str_replace性能比较
php中字符串函数strtr, str_replace和preg_replace的效率对比
php字符串替换(str_replace)
提高PHP运行效率的十二条法则
php代码怎么优化?
php中如何去除字符串中间的空格
PHP删除HTMl标签多种方法
php删除字符串中空格的四种方法
php如何做sql过滤
优化PHP代码的40条建议

[关闭]
~ ~