PHP Function Reference

PHP strtr() Function



The PHP strtr() function returns a string with the replaced values. This function has two versions:

The first version requires three arguments and it is used to replace a set of characters with another set of characters. Every occurrence of any character specified in from is replaced by corresponding character in to. If from and to have different lengths, the extra characters in the longer of the two are ignored. Once a character has been replaced, its new value is not searched again.

The second version requires two arguments and it is used to replace a set of substrings with another set of substrings. The second argument should be an array with keys as what substring to change and values as what substring to change into. The longest keys is replaced first. Once a substring has been replaced, its new value is not searched again. The keys and the values may have any length, provided that there is no empty key. If the array contains an empty string (""), the element is ignored and as of PHP 8.0.0 E_WARNING is raised.

Syntax

//version 1
strtr(string, from, to)

//version 2
strtr(string, array)

Parameters

string Required. Specify the string being searched and replaced on.
from Required. Specify what characters to change.
to Required. Specify what characters to change into.
array Required. Specify an associative with keys as what substring to change and values as what substring to change into.
If the array contains a key which is an empty string (""), the element is ignored and as of PHP 8.0.0 E_WARNING is raised.

Return Value

Returns a string with the replaced values.

Example: strtr() example with three arguments

When three arguments are passed, every occurrence of characters specified in second argument is replaced by corresponding character in third argument. Consider the following example:

<?php
$str = "Hekkp Wprkd";

$retval = strtr($str, "pk", "ol");
echo $retval;
?>

The output of the above code will be:

Hello World

Example: strtr() example with two arguments

When two arguments are passed, then the second argument must be an associative array containing key as substring that need to be replaced and values as replacement substring. Consider the example below:

<?php
$str = "Hello World";
$replace = array("Hello" => "Hi", "World" => "PHP");

$retval = strtr($str, $replace);
echo $retval;
?>

The output of the above code will be:

Hi PHP

Example: replaced values are not searched again

In both version of the function, the replaced values are not searched again. Consider the example below:

<?php
$str1 = "AAABBBCCC";;

$retval1 = strtr($str1, "AC", "CA");
echo $retval1;

echo "\n";

$str2 = "hi all, I said hello";
$replace = array("hello" => "hi", "hi" => "hello");

$retval2 = strtr($str2, $replace);
echo $retval2;
?>

The output of the above code will be:

CCCBBBAAA
hello all, I said hi

Example: strtr() behavior comparison

Consider the example below where both version of the function are used on a same string.

<?php
$str = "AABB";;

echo strtr($str, "AB", "XY")."\n";
echo strtr($str, array("AB" => "XY"));
?>

The output of the above code will be:

XXYY
AXYB

❮ PHP String Reference