php 何时使用 strtr 与 str_replace?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8177296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When to use strtr vs str_replace?
提问by andrewtweber
I'm having a hard time understanding when strtr
would be preferable to str_replace
or vice versa. It seems that it's possible to achieve the exact same results using either function, although the order in which substrings are replaced is reversed. For example:
我很难理解什么时候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');
This outputs
这输出
YeXY XYring
YeZ Zring
YeXY XYring
YeZ Zring
Aside from syntax, is there any benefit to using one over the other? Any cases where one would not be sufficient to achieve a desired result?
除了语法之外,使用一个比另一个有什么好处吗?在任何情况下都不足以达到预期的结果?
回答by Nicolás Ozimica
First difference:
第一个区别:
An interesting example of a different behaviour between strtr
and str_replace
is in the comments section of the PHP Manual:
PHP 手册的注释部分是strtr
和之间不同行为的一个有趣示例str_replace
:
<?php
$arrFrom = array("1","2","3","B");
$arrTo = array("A","B","C","D");
$word = "ZBB2";
echo str_replace($arrFrom, $arrTo, $word);
?>
- I would expect as result: "ZDDB"
- However, this return: "ZDDD" (Because B = D according to our array)
- 我期望的结果是:“ZDDB”
- 然而,这个返回:“ZDDD”(因为根据我们的数组 B = D)
To make this work, use "strtr" instead:
要完成这项工作,请改用“strtr”:
<?php
$arr = array("1" => "A","2" => "B","3" => "C","B" => "D");
$word = "ZBB2";
echo strtr($word,$arr);
?>
- This returns: "ZDDB"
- 这将返回:“ZDDB”
This means that str_replace
is a more global approach to replacements, while strtr
simply translates the chars one by one.
这意味着这str_replace
是一种更全局的替换方法,而strtr
只是一个一个地翻译字符。
Another difference:
另一个区别:
Given the following code (taken from PHP String Replacement Speed Comparison):
鉴于以下代码(取自PHP 字符串替换速度比较):
<?php
$text = "PHP: Hypertext Preprocessor";
$text_strtr = strtr($text
, array("PHP" => "PHP: Hypertext Preprocessor"
, "PHP: Hypertext Preprocessor" => "PHP"));
$text_str_replace = str_replace(array("PHP", "PHP: Hypertext Preprocessor")
, array("PHP: Hypertext Preprocessor", "PHP")
, $text);
var_dump($text_strtr);
var_dump($text_str_replace);
?>
The resulting lines of text will be:
生成的文本行将是:
string(3) "PHP"
string(27) "PHP: Hypertext Preprocessor"
string(3) "PHP"
string(27) "PHP:超文本预处理器"
The main explanation:
主要解释:
This happens because:
发生这种情况是因为:
strtr: it sorts its parameters by length, in descending order, so:
- it will give "more importance" to the largest one, and then, as the subject text is itself the largest key of the replacement array, it gets translated.
- because all the chars of the subject text have been replaced, the process ends there.
str_replace: it works in the order the keys are defined, so:
- it finds the key “PHP” in the subject text and replaces it with: “PHP: Hypertext Preprocessor”, what gives as result:
“PHP: Hypertext Preprocessor: Hypertext Preprocessor”.
then it finds the next key: “PHP: Hypertext Preprocessor” in the resulting text of the former step, so it gets replaced by "PHP", which gives as result:
“PHP: Hypertext Preprocessor”.
there are no more keys to look for, so the replacement ends there.
- it finds the key “PHP” in the subject text and replaces it with: “PHP: Hypertext Preprocessor”, what gives as result:
strtr:它按长度降序对其参数进行排序,因此:
- 它会给最大的一个“更重要”,然后,由于主题文本本身就是替换数组的最大键,它被翻译。
- 因为主题文本的所有字符都被替换了,所以过程到此结束。
str_replace:它按照定义键的顺序工作,因此:
- 它在主题文本中找到键“PHP”并将其替换为:“PHP:超文本预处理器”,结果如下:
“PHP:超文本预处理器:超文本预处理器”。
然后它在上一步的结果文本中找到下一个键:“PHP: Hypertext Preprocessor”,因此它被替换为“PHP”,结果如下:
“PHP:超文本预处理器”。
没有更多要查找的键,因此替换就到此为止了。
- 它在主题文本中找到键“PHP”并将其替换为:“PHP:超文本预处理器”,结果如下:
回答by hakre
It seems that it's possible to achieve the exact same results using either function
似乎可以使用任一函数获得完全相同的结果
That's not always true and depends on the search and replace data you provide. For example where the two function differ see: Does PHP str_replace have a greater than 13 character limit?
这并不总是正确的,取决于您提供的搜索和替换数据。例如,这两个函数的不同之处请参见:Does PHP str_replace has a more than 13 characters limit?
strtr
will not replace in parts of the string that already have been replaced -str_replace
will replace inside replaces.strtr
will start with the longest key first in case you call it with two parameters -str_replace
will replace from left to right.str_replace
can return the number of replacements done -strtr
does not offer such a count value.
strtr
不会替换已经替换的字符串部分 -str_replace
将替换内部替换。strtr
如果您使用两个参数调用它,将首先从最长的键开始 -str_replace
将从左到右替换。str_replace
可以返回完成的替换次数 -strtr
不提供这样的计数值。
回答by Arif RH
I think strtr
provides more flexible and conditional replacement when used with two arguments, for example: if string is 1, replace with a, but if string is 10, replace with b. This trick could only be achieved by strtr
.
我认为strtr
与两个参数一起使用时提供更灵活的条件替换,例如:如果字符串为 1,则替换为 a,但如果字符串为 10,则替换为 b。这个技巧只能通过strtr
.
$string = "1.10.0001";
echo strtr($string, array("1" => "a", "10" => "b"));
// a.b.000a
see : Php Manual Strtr.
请参阅:PHP 手册 Strtr。
回答by Null None
Notice in manual STRTR-- Description string strtr ( string $str , string $from , string $to ) string strtr ( string $str , array $replace_pairs ) If given three arguments, this function returns a copyof str where ...
手册中的注意 STRTR-- 说明 string strtr ( string $str , string $from , string $to ) string strtr ( string $str , array $replace_pairs ) 如果给定三个参数,则此函数返回str的副本,其中 ...
STR_REPLACE-- ... If search or replace are arrays, their elements are processed first to last. ...
STR_REPLACE-- ... 如果搜索或替换是数组,则首先处理它们的元素到最后。...
STRTR each turn NOT effect to next, BUT STR_REPLACE does.
STRTR 每一回合都不会影响下一回合,但 STR_REPLACE 会。