"Mastering PHP: str_replace Function Examples for String Manipulation"

Sandra Jun 29, 2026

Mastering String Replacement: A Comprehensive Guide to PHP's str_replace Function

Using the str_replace function in PHP
Using the str_replace function in PHP

In the vast landscape of web development, PHP plays a pivotal role, and its string manipulation functions are among the most frequently used. One such function is str_replace, a powerful tool designed to replace specific text within strings. Let's dive into the world of PHP string replacement, exploring the str_replace function with practical examples.

String Function - str_replace()| String Replace | PHP Tutorials for beginners - 6 | HINDI
String Function - str_replace()| String Replace | PHP Tutorials for beginners - 6 | HINDI

Understanding str_replace: The Basics

At its core, the str_replace function in PHP is used to replace all occurrences of a search string with a replacement string. It returns the modified string, or the original string if no replacements were made. The function's syntax is straightforward:

Replace Specific Characters in String in R (4 Examples) | gsub, sub & str_replace [stringr Package]
Replace Specific Characters in String in R (4 Examples) | gsub, sub & str_replace [stringr Package]

string str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )

Here, $search is the string or array of strings to search for, $replace is the string or array of strings to replace the search string with, and $subject is the string or an array of strings to search and replace in.

str_replace vs str_ireplace: A Quick Comparison

Simple Edit Data Using PHP and MYSQL [Responsive Page]
Simple Edit Data Using PHP and MYSQL [Responsive Page]

Before we delve into examples, it's essential to understand the difference between str_replace and its case-insensitive counterpart, str_ireplace. While str_replace is case-sensitive, str_ireplace is not. This means that str_ireplace will replace strings regardless of their case, making it a useful alternative when case sensitivity is not a concern.

str_replace in Action: Practical Examples

Single String Replacement

5 Best Free PHP Script Resources Online
5 Best Free PHP Script Resources Online

Let's start with a simple example where we replace all occurrences of "world" with "PHP" in the string "Hello, world!".

php
$str = "Hello, world!";
$new_str = str_replace("world", "PHP", $str);
echo $new_str; // Output: Hello, PHP!

Replacing Multiple Strings

You can also replace multiple strings at once by passing an array to the $search and $replace parameters.

PHP Shorthand If ElseIf Else with Example Code
PHP Shorthand If ElseIf Else with Example Code

php
$str = "Hello, world! This is a test.";
$search = array("world", "test");
$replace = array("PHP", "example");
$new_str = str_replace($search, $replace, $str);
echo $new_str; // Output: Hello, PHP! This is an example.

Replacing Strings in an Array

To replace strings in an array of strings, simply pass the array to the $subject parameter.

a computer screen with different types of text and symbols on it, including an image of a laptop
a computer screen with different types of text and symbols on it, including an image of a laptop
Sort An Array PHP (With Example Programs)
Sort An Array PHP (With Example Programs)
an image of data fetching ssg, srr and isr text on a white background
an image of data fetching ssg, srr and isr text on a white background
How to Fix WordPress RSS Feed Errors (Step-by-Step Tutorial)
How to Fix WordPress RSS Feed Errors (Step-by-Step Tutorial)
Python Programming
Python Programming
the different types of switches are shown in this diagram, with instructions on how to use
the different types of switches are shown in this diagram, with instructions on how to use
My Programming School – Learn Something New Today
My Programming School – Learn Something New Today
owasp top 10 web application vulnerabilities
owasp top 10 web application vulnerabilities
an ad with the words advanced srf and redirection tricks on it, including instructions
an ad with the words advanced srf and redirection tricks on it, including instructions
şömine
şömine
the flow diagram shows how to use react router
the flow diagram shows how to use react router
formatR - Yihui Xie | 谢益辉
formatR - Yihui Xie | 谢益辉
a white screen with the words hitting the recursion error
a white screen with the words hitting the recursion error
Node-RED Context erklärt: Werte speichern ohne Datenbank
Node-RED Context erklärt: Werte speichern ohne Datenbank
WordPress Shortcodes: A Complete Guide — Smashing Magazine
WordPress Shortcodes: A Complete Guide — Smashing Magazine
React Data Fetching Patterns 2026 — The Modern Way to Load Data
React Data Fetching Patterns 2026 — The Modern Way to Load Data
a drawing of a living room with a fire place
a drawing of a living room with a fire place
Fireplace Tile, Living Room Inspiration, Room Inspiration, Fireplace, Living Room
Fireplace Tile, Living Room Inspiration, Room Inspiration, Fireplace, Living Room
an image of a fireplace with measurements for the front and back sides, including the mantle
an image of a fireplace with measurements for the front and back sides, including the mantle

php
$strings = array("Hello, world!", "This is a test.", "Another string.");
$search = "world";
$replace = "PHP";
$new_strings = array_map(function($str) use ($search, $replace) {
    return str_replace($search, $replace, $str);
}, $strings);
print_r($new_strings);
/* Output:
Array
(
    [0] => Hello, PHP!
    [1] => This is a test.
    [2] => Another string.
)
*/

Using str_replace with Regular Expressions

You can also use str_replace with regular expressions to perform more complex string replacements. Here's an example that replaces all email addresses with "[email protected]":

php
$str = "Send your feedback to john@example.com or jane.doe@gmail.com.";
$pattern = '/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/';
$replace = "[email protected]";
$new_str = preg_replace($pattern, $replace, $str);
echo $new_str; // Output: Send your feedback to [email protected] or [email protected].

Performance Considerations

While str_replace is generally fast, it can become a performance bottleneck when used excessively or with large strings. To optimize performance, consider the following:

  • Use str_replace sparingly and only when necessary.
  • Minimize the number of replacements by combining related replacements into a single call.
  • If you're performing many string manipulations, consider using a string manipulation library or extension for better performance.

Conclusion

Mastering PHP's str_replace function is essential for any web developer looking to manipulate strings effectively. With its ability to replace single and multiple strings, as well as its compatibility with regular expressions, str_replace is a versatile tool that every PHP developer should have in their arsenal.