strtr

(PHP 4, PHP 5, PHP 7)

strtrת��ָ���ַ�

˵��

strtr ( string $str , string $from , string $to ) : string
strtr ( string $str , array $replace_pairs ) : string

�ú������� str ��һ�������������� from ��ָ�����ַ�ת��Ϊ to ����Ӧ���ַ��� ���磬 $from[$n]��ÿ�εij��ֶ��ᱻ�滻Ϊ $to[$n]������ $n ��������������Ч��λ��(offset)��

��� from �� to ���Ȳ���ȣ���ô������ַ����ֽ������ԡ� str �ij��Ƚ���ͷ��ص�ֵһ����

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

�μ�