PHP strtr() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, strtr() translates characters or replaces whole substrings in a string.

It works with two strings (character by character) or with an array of search and replace pairs.

Syntax

Call strtr() with the values you need. Start with this line.

PHP
$new = strtr($text, ["{name}" => "Asha"]);
CallWhat it does
strtr("Hi all", "ai", "eo")Replaces a with e and i with o. Returns "Ho ell".
strtr("Hi all", ["Hi" => "Hello"])Replaces the substring "Hi". Returns "Hello all".

With the array form, strtr() tries the longest keys first and never searches inside text it has already replaced.

Library declarations
PHP
strtr(string $string, string $from, string $to): string
strtr(string $string, array $replace_pairs): string

Parameters

ParameterDescription
stringRequired. The input string.
fromRequired in the three-argument form. The characters to replace.
toRequired in the three-argument form. The replacement characters. If it is shorter than from, extra characters in from are ignored.
replace_pairsRequired in the two-argument form. An array like ["old" => "new"].

Return Value

Return ValueDescription
stringThe translated string. The original string is returned if nothing matched.

PHP Examples

Example 1 — Translate Characters

With three arguments, each character in $from becomes the matching character in $to.

PHP
<?php
$text = "Hi all";
echo strtr($text, "ai", "eo"), PHP_EOL;

How It Works

  1. Map the characters. a becomes e and i becomes o.
  2. Go letter by letter. "Hi" turns into "Ho".
  3. Keep the rest. Spaces and unmapped letters like l stay as they are.
  4. Result. "all" turns into "ell", so you see Ho ell.

Example 2 — Swap Two Words Safely

Because strtr() never rescans replaced text, you can swap values in a single call.

PHP
<?php
$text = "Hi all, Hello Bob";
$swapped = strtr($text, ["Hi" => "Hello", "Hello" => "Hi"]);

echo $swapped, PHP_EOL;

How It Works

  1. Two pairs. "Hi" becomes "Hello" and "Hello" becomes "Hi".
  2. Longest first. PHP checks the longer key before the shorter one.
  3. No second pass. New text is never searched again, so there is no ping-pong.
  4. Compare. Chaining str_replace() would turn both words into the same value.

Example 3 — Fill a Template

Replace placeholders in a message with real values.

PHP
<?php
$tpl = "Dear {name}, your order {id} has shipped.";
$msg = strtr($tpl, [
    "{name}" => "Asha",
    "{id}"   => "#1024",
]);

echo $msg, PHP_EOL;

How It Works

  1. Write a template. Placeholders sit inside curly braces.
  2. Build a map. Each key is a placeholder and each value is the real text.
  3. Call strtr(). Every placeholder is replaced in one pass.
  4. Print the message. The final text has no braces left.

Frequently Asked Questions

strtr() translates characters or replaces substrings. You can give it two strings of characters or an array of replacement pairs.
strtr(string $string, string $from, string $to): string, or strtr(string $string, array $replace_pairs): string.
strtr() never searches inside text it already replaced and it tries longer keys first. str_replace() processes replacements one after another, so earlier changes can be changed again.
PHP only uses as many characters as the shorter of the two strings. The rest are ignored.
Yes. "Hi" and "hi" are different keys. Normalize the text first if you need case-insensitive replacement.

Next: substr()

substr() returns a part of a string, starting at a position you choose.

substr() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful