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
Syntax
strtr(string $string, string $from, string $to): string | strtr(string $string, array $replace_pairs): string
Parameters
| Parameter | Description |
|---|---|
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
<?php
echo strtr("Hi all, I said hello", ["Hi" => "Hello", "hello" => "hi"]), "\n";
Output
Hello all, I said hi
<?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
| Version | Change |
|---|---|
| PHP 4.0 | Added |
In other languages
str.translate()· Python
Related
str_replace()· Replace every occurrence of a search string with a replacement. Accepts arrays to make several replacements at once.- PHP Strings · tutorial