PHP str_replace() Function: Basic and Advanced Usage

This article explains the PHP str_replace() function, covering its syntax, basic string replacement, handling multiple replacements with arrays, case‑insensitive alternatives, and how to retrieve the number of replacements performed.

php Courses
php Courses
php Courses
PHP str_replace() Function: Basic and Advanced Usage

In PHP programming we often need to manipulate strings, such as replacing characters or substrings, and the str_replace() function is a very common tool. This article details how to use this function and important considerations.

str_replace() Function

The str_replace() function is a native PHP function used to replace specified characters or substrings within a string. Its syntax is: str_replace($search, $replace, $subject); Here, $search is the character or string to be replaced, $replace is the replacement, and $subject is the original string to be processed. The function returns the string after replacement. Both $search and $replace can be a string or an array, enabling more complex usage.

Basic Usage of str_replace()

A simple example demonstrates the basic usage:

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

This replaces the word “美妙” with “优美” in the original string, producing a new string.

Advanced Usage of str_replace()

1. Replacing Multiple Characters or Substrings

Beyond the basic usage, str_replace() can replace multiple characters or substrings by passing arrays for $search and $replace:

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

The function iterates over each element in the search array and replaces it with the corresponding element in the replace array.

2. Case‑Sensitive vs. Case‑Insensitive Replacement

If you need a case‑insensitive replacement, use the variant str_ireplace():

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

Here, str_ireplace() ignores case, replacing “PHP” with “JAVA”.

3. Returning the Number of Replacements

To know how many replacements were performed, provide a fourth argument to capture the count:

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

The variable $count receives the number of times a replacement occurred.

The str_replace() function is a versatile PHP string function that can handle single or multiple replacements, case‑sensitivity control, and can report how many replacements were made.

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.

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