PHP strtr() Function
Definition
The strtr() function translates certain characters in a string.
Syntax
PHP strtr() Function has the following syntax.
strtr(string,from,to)
or
strtr(string,array)
Parameter
Parameter | Is Required | Description |
---|---|---|
string | Required. | String to translate |
from | Required (unless array is used). | What characters to change |
to | Required (unless array is used). | What characters to change into |
array | Required (unless to and from is used). | An array containing what to change from as key, and what to change to as value |
Return
PHP strtr() Function returns the translated string.
Note
If the from and to parameters are different in length, both will be trimmed to the length of the shortest.
Example
Replace the characters "jav" in the string with "PHP":
<?php
echo strtr("java2s . com ","jav","PHP");
?>
The code above generates the following result.
Example 2
Replace the string "Hello world" with "Hi java2s.com":
<?php
$arr = array("Hello" => "Hi", "world" => "java2s.com");
echo strtr("Hello world",$arr);
?>
The code above generates the following result.