Master PHP’s strtr(): Powerful String Replacement Techniques

This article explains how to use PHP's strtr() function for various string replacement scenarios, including simple character swaps, multiple replacements with associative arrays, sequence replacements, and partial removals, providing clear code examples for each use case.

php Courses
php Courses
php Courses
Master PHP’s strtr(): Powerful String Replacement Techniques

In PHP programming, the strtr() function is a useful string replacement function. It replaces specified characters or strings in a string with other characters or strings. This article introduces the usage of strtr() and provides concrete code examples.

Basic syntax of strtr()

strtr(string $str, array $replace)

Here, $str is the original string to be processed, and $replace is a set of characters or strings used for replacement. $replace can be an associative or indexed array, where each key‑value pair represents the string to replace and its replacement.

Different usages of strtr()

Simple character replacement

$text = "Hello World";
echo strtr($text, "World", "PHP"); // output: Hello PHP

In this example, we replace "World" in $text with "PHP" and output the result.

Replacing multiple characters

$text = "I like apples and oranges.";
$replace = array("apples" => "bananas", "oranges" => "grapes");
echo strtr($text, $replace); // output: I like bananas and grapes.

Here, $replace is an associative array mapping strings to their replacements. The example replaces "apples" with "bananas" and "oranges" with "grapes".

Replacing character sequences

$text = "I love PHP programming.";
$replace = array("PHP" => "Java", "programming" => "coding");
echo strtr($text, $replace); // output: I love Java coding.

This example replaces "PHP" with "Java" and "programming" with "coding".

Partial character replacement

$text = "I am a programmer.";
$replace = array("a" => "", "r" => "");
echo strtr($text, $replace); // output: I m pogamm.

In this case, we remove the characters "a" and "r" from $text, resulting in the displayed output.

The strtr() function is a powerful tool for string replacement in PHP, offering flexibility for simple character swaps, multiple replacements, sequence replacements, and partial removals.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPCode Examplesstring-replacementstrtr
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.