Questions tagged «strtr»

4
何时使用strtr vs str_replace?
我很难理解何时strtr会更合适,str_replace反之亦然。尽管替换子字符串的顺序相反,但是使用这两个函数似乎都可能获得完全相同的结果。例如: echo strtr('test string', 'st', 'XY')."\n"; echo strtr('test string', array( 's' => 'X', 't' => 'Y', 'st' => 'Z' ))."\n"; echo str_replace(array('s', 't', 'st'), array('X', 'Y', 'Z'), 'test string')."\n"; echo str_replace(array('st', 't', 's'), array('Z', 'Y', 'X'), 'test string'); 这个输出 YeXY XYring YeZ Zring YeXY XYring YeZ Zring 除了语法之外,使用一种语法还有什么好处?在任何情况下都不足以达到预期的结果吗?
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.