strtr
(PHP 4, PHP 5, PHP 7, PHP 8)
Translate characters or replace substrings
Description
Alternative signature (not supported with named arguments):
If given three arguments, this function returns a copy of string where all occurrences of each (single-byte) character in from have been translated to the corresponding character in to, i.e., every occurrence of $from[$n] has been replaced with $to[$n], where $n is a valid offset in both arguments.
If from and to have different lengths, the extra characters in the longer of the two are ignored. The length of string will be the same as the return value's.
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 string. However, this function will be the most efficient when all the keys have the same size.
Parameters
stringThe
stringbeing translated.fromThe
stringbeing translated toto.toThe
stringreplacingfrom.replace_pairsThe
replace_pairsparameter may be used instead oftoandfrom, in which case it's anarrayin the formarray('from' => 'to', ...).If
replace_pairscontains a key which is an emptystring(""), the element is ignored; as of PHP 8.0.0E_WARNINGis raised in this case.
Return Values
Returns the translated string.
Examples
strtr() example
<?php
$original = "He1Lo, w0rld!";
// With three arguments, strtr() performs byte-by-byte translation.
$result = strtr($original, "1L0", "llo");
echo $result;The above example will output:
Hello, world!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.
strtr() example with two arguments
<?php
$replace_pairs = ["h" => "-", "hello" => "hi", "hi" => "hello"];
echo strtr("hi all, I said hello", $replace_pairs);The above example will output:
hello all, I said hiThe two modes of behavior are substantially different. With three arguments, strtr() will replace bytes; with two, it may replace longer substrings.
strtr() behavior comparison
<?php
// Single-byte characters
$singlebyte_string = "baab";
echo strtr($singlebyte_string, "ab", "01"), "\n"; // Byte-by-byte translation
$replace_pairs = ["ab" => "01"];
echo strtr($singlebyte_string, $replace_pairs), "\n"; // Substring translation
// Multi-byte characters
$multibyte_string = "Ä"; // In UTF-8, encoded as two bytes: "\xC3\x84"
$result = strtr($multibyte_string, 'Ä', 'A');
echo bin2hex($result), "\n"; // "4184", where "c3" becomes "41" ("A"), while "84" is kept and breaks UTF-8
$replace_pairs = ['Ä' => 'A'];
echo strtr($multibyte_string, $replace_pairs); // A correct multibyte substring translationThe above example will output:
1001
ba01
4184
A