PHP 8 reference · String Functions

PHP strtr() Function

Translate characters, or replace substrings in one pass using an array of from => to pairs. Longest keys match first.

PHP 8 Since 4.0

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

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

Parameters

ParameterDescription
string The input string.
from / to Two strings: each character in from becomes the matching character in to.
replace_pairs An array of search => replacement pairs.

Return value

The translated string.

Examples

Swap words in one passTry it yourself
<?php
echo strtr("Hi all, I said hello", ["Hi" => "Hello", "hello" => "hi"]), "\n";

Output

Hello all, I said hi
Character by characterTry it yourself
<?php
echo strtr("2026-10-22", "-", "/"), "\n";
echo strtr("abc", "ab", "xy"), "\n";

Output

2026/10/22
xyc

Unlike str_replace() with arrays, strtr() never re-replaces text it has already changed, so swapping two words works as expected.

Version history

VersionChange
PHP 4.0Added

In other languages

Was this page helpful?