Tutorialspoint.dev

PHP | strtr() Function

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, $string2) 

or,

strtr($string, $arr)

Paramaters: 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:



  • When $string1 and $string2 are passed as parameters, it returns the translated string by changing the $string1 characters to $string2 characters.
  • If an $array is passed as parameter, it returns the translated string by changing the key string to the value strings. If any of the key is passed as “”, then it returns false as output.

Examples:

Input : $string = "gieuz foh geeks", 
        $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"   ,   $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 demonsrate the strtr() function when same length string1 and string2 is passed.

<?php
// PHP program to demonsrate the strtr() function 
// when same length string1 and string2 is passed
$string = "gieuz foh geeks" ;
$string1 = "iuzh"
$string2 = "eksr";
  
// replacement is done 
echo strtr($string, $string1, $string2);
  
?>

Output:

geeks for geeks

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

<?php
// PHP program to demonsrate the strtr() function 
// when different length string1 and string2 is passed
$string = "gieuz foh geeks" ;
$string1 = "iuzh"
$string2 = "eks";
  
// replacement is done 
echo strtr($string, $string1, $string2);
  
?>

Output:

geeks foh geeks

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

<?php
// PHP program to demonsrate the strtr() function 
// which replaces at all positions where 
// characters are present
$string = "giiks for giiks" ;
$string1 = "i"
$string2 = "e";
  
// replacement is done 
echo strtr($string, $string1, $string2);
  
?>

Output:

geeks for geeks

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

<?php
// PHP program to demonsrate the strtr() function 
// when array is passed as the parameter
  
$string = "giiks in giiks" ;
$arr = array("giiks" => "geeks", "in" => "for");
  
// replacement is done 
echo strtr($string, $arr);
?>

Output:

geeks for geeks

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

<?php
// PHP program to demonsrate the strtr() function 
// when one key in array is passed as ""
  
$string = "giiks in giiks" ;
$arr = array("giiks" => "geeks", "" => "for");
  
// replacement is done 
echo strtr($string, $arr);
?>

Output:

No Output

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



This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter