(PHP 4, PHP 5, PHP 7)
strtr — 转锟斤拷指锟斤拷锟街凤拷
$str
, string $from
, string $to
) : string$str
, array $replace_pairs
) : string
锟矫猴拷锟斤拷锟斤拷锟斤拷 str
锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 from
锟斤拷指锟斤拷锟斤拷锟街凤拷转锟斤拷为 to
锟斤拷锟斤拷应锟斤拷锟街凤拷锟斤拷
锟斤拷锟界, $from[$n]锟斤拷每锟轿的筹拷锟街讹拷锟结被锟芥换为
$to[$n]锟斤拷锟斤拷锟斤拷 $n 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷效锟斤拷位锟斤拷(offset)锟斤拷
锟斤拷锟� from
锟斤拷 to
锟斤拷锟饺诧拷锟斤拷龋锟斤拷锟矫达拷锟斤拷锟斤拷锟街凤拷锟斤拷锟街斤拷锟斤拷锟斤拷锟皆★拷 str
锟侥筹拷锟饺斤拷锟斤拷头锟斤拷氐锟街狄伙拷锟斤拷锟�
If given two arguments, the second should be an array in the form array('from' => 'to', ...). The return value is a string where all the occurrences of the array keys have been replaced by the corresponding values. The longest keys will be tried first. Once a substring has been replaced, its new value will not be searched again.
In this case, the keys and the values may have any length, provided that
there is no empty key; additionally, the length of the return value may
differ from that of str
.
However, this function will be the most efficient when all the keys have the
same size.
str
锟斤拷转锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷
from
锟街凤拷锟斤拷锟斤拷锟诫将要锟斤拷转锟斤拷锟斤拷目锟斤拷锟街凤拷 to
锟斤拷锟接︼拷锟皆达拷址锟斤拷锟�
to
锟街凤拷锟斤拷锟斤拷锟诫将要锟斤拷转锟斤拷锟斤拷锟街凤拷 from
锟斤拷锟接︼拷锟侥匡拷锟斤拷址锟斤拷锟�
replace_pairs
锟斤拷锟斤拷 replace_pairs
锟斤拷锟斤拷锟斤拷锟斤拷取锟斤拷 to
锟斤拷 from
锟斤拷锟斤拷锟斤拷锟斤拷为锟斤拷锟斤拷锟斤拷 array('from' => 'to', ...) 锟斤拷式锟斤拷锟街碉拷锟斤拷锟介。
锟斤拷锟斤拷转锟斤拷锟斤拷锟�锟街凤拷锟斤拷锟斤拷
锟斤拷锟� replace_pairs
锟叫帮拷锟斤拷一锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷""锟斤拷锟斤拷锟斤拷锟斤拷么锟斤拷锟斤拷锟斤拷 FALSE
锟斤拷
If the str
is not a scalar
then it is not typecasted into a string, instead a warning is raised and
NULL
is returned.
Example #1 strtr() 锟斤拷锟斤拷
<?php
$addr = strtr($addr, "???", "aao");
?>
The next example shows the behavior of strtr() when called with only two arguments. Note the preference of the replacements ("h" is not picked because there are longer matches) and how replaced text was not searched again.
Example #2 使锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 strtr() 锟斤拷锟斤拷
<?php
$trans = array("hello" => "hi", "hi" => "hello");
echo strtr("hi all, I said hello", $trans);
?>
锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�
hello all, I said hi
The two modes of behavior are substantially different. With three arguments, strtr() will replace bytes; with two, it may replace longer substrings.
Example #3 strtr() behavior comparison
<?php
echo strtr("baab", "ab", "01"),"\n";
$trans = array("ab" => "01");
echo strtr("baab", $trans);
?>
锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�
1001 ba01