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

ParameterIs RequiredDescription
stringRequired.String to translate
fromRequired (unless array is used).What characters to change
toRequired (unless array is used).What characters to change into
arrayRequired (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.





















Home »
  PHP Tutorial »
    Function reference »




PHP Array Functions
PHP Calendar Functions
PHP Class Functions
PHP Data Type Functions
PHP Date Functions
PHP File Functions
PHP Image Functions
PHP Math Functions
PHP MySQLi Functions
PHP SimpleXML Functions
PHP String Functions
PHP XML Functions
PHP Zip Functions