PHP String strtr() function
The strtr() is an in-built function of PHP, which is used to replace a substring inside the other string. It provides the facility to change the particular word in a string. The strtr() function translates characters or replaces the substrings. It is a case-sensitive function. PHP 4+ versions support this function.
Note: strtr() function replaces a character from all the positions wherever that character is present.
There are some other functions in PHP which are similar to the strtolower() function:
There are two syntaxes available for this function, one for string or character replacement and another one is for array key replacement. These syntaxes are given below.
For string replacement:
There are three arguments in this function. It returns the copy of $str after replacing each character of $from with $to.
For array key replacement:
Here, the above function is containing two arguments. The second argument is $replace_pair, which is in the form of an array('from' => 'to', ?). It will return a string after replacing all the occurrence of array key with the corresponding values.
$str: It is a string parameter which is being translated, means it is the main string which is going to be translated.
$from: It is the next parameter of this function which will be replaced in the string with $to. It is a required parameter unless the array is used.
$to: It is a parameter which will be replaced with $from variable. This parameter is also required as $from unless the array is used.
Note: If from and to have different lengths, then the extra characters of the longer string will be ignored. The length of the returned string $str will be the same.
$replace_pair: This parameter is in the form of an array instead of from and to. This array contains the two strings ($string1 and $string2), i.e., a string1 which needs be changed and string2 to which it is changed.
Note: If string1 and string2 both have different length, then the longer string will be formatted to the length of the shorter string.
There are a few numbers of examples given below. With the help of these examples, we can understand the working of the strtr() function.
Hie! Good Morning
y is replaced with e
u is replaced with o
h is replaced with r
e is replaced with i
So, Hiy! Guud Mohneng is replaced with Hie! Good Morning.
PHP program to demonstrate strtr() function when $from and $to have different length.
Hie! Gord Mohning
y is replaced with e
e is replaced with o
u is replaced with r
h is not replaced with any character.
Now, Hiy! Geud Mohneng is replaced with Hie! Gord Mohning.
Replacement with array keys
Welcome to javaTpoint.
PHP program to demonstrate strtr() function when array key have empty "" string.
Multiple replacements with single letter
Welcome to javaTpoemt.
i is replaced with e at multiple places.
n is replaced with m at multiple places.
C is replaced with T only once because this function is case-sensitive.
So, Wilcone to javaCpoint is replaced with Welcome to javaTpoemt rather than Welcome to JavaTpoint.
In this example, for the first case, all occurrence of q is replaced with o. On the other hand, Q is not replaced with o because as it is a case-sensitive function.
Good health Good Life. Gqqd health Gqqd Life. //case-sensitive