Javatpoint Logo
Javatpoint Logo

PHP String strtr() function

The strtr() is an in-built function of PHP, which is used to replace a substring inside the other string. It provides the facility to change the particular word in a string. The strtr() function translates characters or replaces the substrings. It is a case-sensitive function. PHP 4+ versions support this function.

Note: strtr() function replaces a character from all the positions wherever that character is present.

There are some other functions in PHP which are similar to the strtolower() function:

Syntax

There are two syntaxes available for this function, one for string or character replacement and another one is for array key replacement. These syntaxes are given below.

For string replacement:

There are three arguments in this function. It returns the copy of $str after replacing each character of $from with $to.

For array key replacement:

Here, the above function is containing two arguments. The second argument is $replace_pair, which is in the form of an array('from' => 'to', ?). It will return a string after replacing all the occurrence of array key with the corresponding values.

Parameters

$str: It is a string parameter which is being translated, means it is the main string which is going to be translated.

$from: It is the next parameter of this function which will be replaced in the string with $to. It is a required parameter unless the array is used.

$to: It is a parameter which will be replaced with $from variable. This parameter is also required as $from unless the array is used.

Note: If from and to have different lengths, then the extra characters of the longer string will be ignored. The length of the returned string $str will be the same.

$replace_pair: This parameter is in the form of an array instead of from and to. This array contains the two strings ($string1 and $string2), i.e., a string1 which needs be changed and string2 to which it is changed.

Note: If string1 and string2 both have different length, then the longer string will be formatted to the length of the shorter string.

Return Value

  • It returns the translated string after replacing $from characters with $to characters.
  • If the passed parameter is array ($replace_pair), then it returns the translated string by changing the key string with the corresponding value. If that array parameter contains a key with an empty string (? ?), then it will return FALSE.

Examples

There are a few numbers of examples given below. With the help of these examples, we can understand the working of the strtr() function.

Example 1

Output:

Hie! Good Morning

Explanation

y is replaced with e

u is replaced with o

h is replaced with r

e is replaced with i

So, Hiy! Guud Mohneng is replaced with Hie! Good Morning.

Example 2

PHP program to demonstrate strtr() function when $from and $to have different length.

Output:

Hie! Gord Mohning

Explanation

y is replaced with e

e is replaced with o

u is replaced with r

h is not replaced with any character.

Now, Hiy! Geud Mohneng is replaced with Hie! Gord Mohning.

Example 3

Replacement with array keys

Output:

Welcome to javaTpoint.

Example 4

PHP program to demonstrate strtr() function when array key have empty "" string.

Output:

No output

Example 5

Multiple replacements with single letter

Output:

Welcome to javaTpoemt.

Explanation

i is replaced with e at multiple places.

n is replaced with m at multiple places.

C is replaced with T only once because this function is case-sensitive.

So, Wilcone to javaCpoint is replaced with Welcome to javaTpoemt rather than Welcome to JavaTpoint.

Example 6

Case-sensitive

Output:

In this example, for the first case, all occurrence of q is replaced with o. On the other hand, Q is not replaced with o because as it is a case-sensitive function.

Good health Good Life.
Gqqd health Gqqd Life.     //case-sensitive






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA