您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

PHP中的strtr函数使用介绍(str_replace)

2025/12/26 23:23:30发布23次查看
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, 并且使用了第一种形式, 则一定要注意这个特征, 这可能是一个陷阱.
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product