strtr()

strtr() — Translate characters or replace substrings.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

If given three arguments, this function returns a copy of string where all occurrences of each (single-byte) character in from have been translated to the corresponding character in to, i.e., every occurrence of $from[$n] has been replaced with $to[$n], where $n is a valid offset in both arguments.

Signature

strtr(string $string, string $from, string $to): string strtr(string $string, array $replace_pairs): string

Parameters

ParameterTypeWhat it takes
$stringstringThe string being translated.
$fromstringThe string being translated to to.
$tostringThe string replacing from.
$replace_pairsarrayThe replace_pairs parameter may be used instead of to and from, in which case it's an array in the form array('from' => 'to', ...).

Return value

Returns the translated string.

Example

A runnable snippet:

$original = "He1Lo, w0rld!"; // With three arguments, strtr() performs byte-by-byte translation. $result = strtr($original, "1L0", "llo"); echo $result;

Output:

Hello, world!

See also

str_replace(), preg_replace()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page strtr() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.