Master PHP’s str_replace: Simple Replacements, Bulk Swaps, and Deletions

Learn how to use PHP’s str_replace function for efficient string manipulation, covering basic replacements, multiple character swaps, and character removal with clear code examples and explanations to boost your backend development skills.

php Courses
php Courses
php Courses
Master PHP’s str_replace: Simple Replacements, Bulk Swaps, and Deletions

In PHP, strings are a common data type, and sometimes you need to replace or delete certain characters within a string. For this, PHP provides a very convenient function str_replace (string replacement) to accomplish the task.

The syntax of the str_replace function is as follows: str_replace($search, $replace, $subject); This function searches for $search in the string $subject and replaces it with $replace.

Below we look at some specific examples demonstrating the use of the str_replace function.

Example 1: Simple replacement

$text = "今天是星期一,明天是星期二,后天是星期三。";
$new_text = str_replace("星期一", "周一", $text);
echo $new_text;

Result:

今天是周一,明天是星期二,后天是星期三。

In this example, we replaced the original string’s "星期一" with "周一".

Example 2: Replace multiple characters

$text = "The quick brown fox jumps over the lazy dog.";
$new_text = str_replace(array("quick", "brown", "lazy"), "slow", $text);
echo $new_text;

Result: The slow fox jumps over the slow dog. Here we used an array as the replacement parameter, swapping the words "quick", "brown", and "lazy" with "slow".

Example 3: Delete specific characters

$text = "Hello, world!";
$new_text = str_replace("o", "", $text);
echo $new_text;

Result: Hell, wrld! In this example, we replaced the character "o" with an empty string, effectively deleting the "o" character.

The str_replace function also has other usages, such as specifying the number of replacements, case sensitivity, etc. You can refer to the official PHP documentation for more details.

In summary, the str_replace function is a very useful string replacement function that can conveniently replace or delete specific characters in a string. Mastering this function can help you handle string‑related operations more efficiently and improve code readability.

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 Manipulationstr_replace
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.