Tuesday, September 24, 2024
Google search engine
HomeLanguagesPHP strtr() Function

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)

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: 
 

  • 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 neveropen", 
        $string1 = "iuzh"   ,    $string2="eksr"
Output : neveropen for neveropen
Explanation : i replaced by e 
u replaced by k 
z replaced by s 
h replaced by r 

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

Input: $string = "giiks in giiks",
       $arr = array("giiks" => "neveropen", "in" => "for")
Output: neveropen for neveropen  
Explanation: "giiks" was replaced by "neveropen" 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
// PHP program to demonstrate the strtr() function 
// when same length string1 and string2 is passed
$string = "gieuz foh neveropen" ;
$string1 = "iuzh"
$string2 = "eksr";
  
// replacement is done 
echo strtr($string, $string1, $string2);
  
?>


Output: 
 

neveropen for neveropen

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

php




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


Output: 
 

neveropen foh neveropen

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

php




<?php
// PHP program to demonstrate 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: 
 

neveropen for neveropen

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

php




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


Output: 
 

neveropen for neveropen

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

php




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


Output: 
 

No Output

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

RELATED ARTICLES

Most Popular

Recent Comments