The strtr() function translates certain characters in a string.
PHP strtr() Function has the following syntax.
strtr(string,from,to)
or
strtr(string,array)
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 |
PHP strtr() Function returns the translated string.
If the from and to parameters are different in length, both will be trimmed to the length of the shortest.
Replace the characters "jav" in the string with "PHP":
<?php
echo strtr("java2s . com ","jav","PHP");
?>
The code above generates the following result.
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.