Master PHP’s str_replace(): From Basics to Advanced Tricks

This article explains PHP’s native str_replace() function, covering its syntax, basic usage, and advanced techniques such as array replacements, case‑insensitive replacements with str_ireplace(), and retrieving the number of replacements performed, all illustrated with clear code examples.

php Courses
php Courses
php Courses
Master PHP’s str_replace(): From Basics to Advanced Tricks

In PHP programming, handling strings often requires replacing characters or substrings. The native str_replace() function is widely used for this purpose.

str_replace() function

The str_replace() function replaces specified characters or substrings in a string. Its syntax is: str_replace($search, $replace, $subject); Here $search is the value to be replaced, $replace is the replacement, and $subject is the original string. Both $search and $replace can be strings or arrays, enabling more complex usage.

Basic usage of str_replace()

Example:

<?php
$str = "php就是如此的美妙!";
$new_str = str_replace("美妙", "优美", $str);
echo $new_str; // 输出:“php就是如此的优美!”
?>

This replaces “美妙” with “优美” in the original string.

Advanced usage of str_replace()

1. Replacing multiple characters or substrings

You can pass arrays to $search and $replace:

<?php
$str = "PHP是一种很不错的语言,值得学习!";
$search = array("PHP", "值得", "!");
$replace = array("php", "很值得", ".");
$new_str = str_replace($search, $replace, $str);
echo $new_str; // 输出:“php是一种很不错的语言,很值得学习.”
?>

This replaces each element of the search array with the corresponding element in the replace array.

2. Case-sensitive replacement

To perform a case‑insensitive replacement, use str_ireplace():

<?php
$str = "PHP是一种很不错的语言,值得学习!";
$new_str = str_ireplace("php", "JAVA", $str);
echo $new_str; // 输出:“JAVA是一种很不错的语言,值得学习!”
?>

3. Getting the number of replacements

The fourth parameter of str_replace() returns the count of replacements performed:

<?php
$str = "PHP是一种很不错的语言,值得学习!";
$new_str = str_replace("PHP", "JAVA", $str, $count);
echo $new_str . " 替换了" . $count . "次"; // 输出:“JAVA是一种很不错的语言,值得学习!替换了1次”
?>

After execution, $count holds the number of replacements.

The str_replace() function is a flexible PHP string function that can handle single or multiple replacements, case sensitivity, and return the number of replacements.

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 Examplesbackend-developmentString Manipulationphp-tutorialstr_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.