PHP strtr() Function (original) (raw)

Last Updated : 22 Jun, 2023

The strtr() is an inbuilt function in PHP which is used to replace a substring in a string to a given string of characters. It also has the option to change a particular word to a different word in a string. The function is case sensitive.
Syntax:

strtr($string, string1,string1, string1,string2)

or,

strtr($string, $arr)

Parameters: This function accepts three parameters as shown in the above syntax and described below:

  1. $string: It specifies the string in which replacement is to be done. It is a mandatory parameter.
  2. $string1: It specifies the string of characters which has to be replaced if present in the $string. This is a mandatory parameter if array is not used.
  3. $string2: It specifies the string of characters to which the characters of $string1 are to be changed. This is a mandatory parameter if array is not used.
  4. $arr: We can pass either of ($string1 and $string2) or $array as parameter. Array is passed as the parameter when we want to change any particular substring. The $array contains the string to be changed and the string to which it is changed.

Note: When $string1 and $string2 are of different length, then the longer string will be formatted to the length of the shorter one.
Return Value: The return value of this function depends on two cases:

Examples:

Input : $string = "gieuz foh geeks", string1="iuzh",string1 = "iuzh" , string1="iuzh",string2="eksr" Output : geeks for geeks Explanation : i replaced by e u replaced by k z replaced by s h replaced by r

Input : $string = "gieuz foh geeks", string1="iuzh",string1 = "iuzh" , string1="iuzh",string2 = "eks" Output : geeks foh geeks Explanation: "iuzh" was reduced to "iuz" and then replacement was done.

Input: $string = "giiks in giiks", $arr = array("giiks" => "geeks", "in" => "for") Output: geeks for geeks
Explanation: "giiks" was replaced by "geeks" and "in" by "for"

Below programs illustrate the strtr() function in PHP:
Program 1: Program to demonstrate the strtr() function when same length string1 and string2 is passed.

php

<?php

$string = "gieuz foh geeks" ;

$string1 = "iuzh" ;

$string2 = "eksr" ;

echo strtr ( $string , $string1 , $string2 );

?>

Output:

geeks for geeks

Program 2: Program to demonstrate the strtr() function when different length string1 and string2 is passed.

php

<?php

$string = "gieuz foh geeks" ;

$string1 = "iuzh" ;

$string2 = "eks" ;

echo strtr ( $string , $string1 , $string2 );

?>

Output:

geeks foh geeks

Program 3: Program to demonstrate the strtr() function which replaces at all positions where characters are present.

php

<?php

$string = "giiks for giiks" ;

$string1 = "i" ;

$string2 = "e" ;

echo strtr ( $string , $string1 , $string2 );

?>

Output:

geeks for geeks

Program 4: Program to demonstrate the strtr() function when array is passed as the parameter.

php

<?php

$string = "giiks in giiks" ;

$arr = array ( "giiks" => "geeks" , "in" => "for" );

echo strtr ( $string , $arr );

?>

Output:

geeks for geeks

Program 5: Program to demonstrate the strtr() function when one key in array is passed as “”.

php

<?php

$string = "giiks in giiks" ;

$arr = array ( "giiks" => "geeks" , "" => "for" );

echo strtr ( $string , $arr );

?>

Output:

No Output

Reference:
http://php.net/manual/en/function.strtr.php