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.
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:
<code>str_replace($search, $replace, $subject);</code>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:
<code><?php
$str = "php就是如此的美妙!";
$new_str = str_replace("美妙", "优美", $str);
echo $new_str; // Outputs: “php就是如此的优美!”
?></code>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 :
<code><?php
$str = "PHP是一种很不错的语言,值得学习!";
$search = array("PHP", "值得", "!");
$replace = array("php", "很值得", ".");
$new_str = str_replace($search, $replace, $str);
echo $new_str; // Outputs: “php是一种很不错的语言,很值得学习.”
?></code>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() :
<code><?php
$str = "PHP是一种很不错的语言,值得学习!";
$new_str = str_ireplace("php", "JAVA", $str);
echo $new_str; // Outputs: “JAVA是一种很不错的语言,值得学习!”
?></code>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:
<code><?php
$str = "PHP是一种很不错的语言,值得学习!";
$new_str = str_replace("PHP", "JAVA", $str, $count);
echo $new_str . "替换了" . $count . "次"; // Outputs: “JAVA是一种很不错的语言,值得学习!替换了1次”
?></code>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.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.