"Mastering PHP: str_replace All in a Flash"

Sandra Jun 29, 2026

In the vast landscape of web development, PHP's string manipulation functions play a pivotal role. Among them, str_replace() is a powerhouse that enables developers to replace specific text within a string. But what if you need to replace all occurrences, not just the first? Let's delve into the world of PHP's str_replace() and explore how to replace all occurrences of a string.

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

Understanding str_replace()

two page spreads with numbers and symbols for each section in the text, which includes information about
two page spreads with numbers and symbols for each section in the text, which includes information about

Before we dive into replacing all occurrences, let's ensure we understand the basic syntax of str_replace(). The function takes three arguments: the search string, the replacement string, and the subject string (the string in which you want to perform the replacement).

Here's a simple example:

a document with yellow lines on it
a document with yellow lines on it

```php $subject = "Hello, World!"; $search = "World"; $replace = "PHP"; echo str_replace($search, $replace, $subject); // Outputs: Hello, PHP! ```

Replacing All Occurrences with str_replace()

By default, str_replace() only replaces the first occurrence of the search string. To replace all occurrences, you can use a simple trick with regular expressions. You need to add an 's' modifier to the search string, which stands for 'global'. This modifier tells PHP to replace all occurrences, not just the first.

REST API Basics Explained — Simple Developer Guide (2025)
REST API Basics Explained — Simple Developer Guide (2025)

Here's how you can do it:

```php $subject = "Hello, World! World is wonderful."; $search = "World"; $replace = "PHP"; echo str_replace($search, $replace, $subject); // Outputs: Hello, PHP! PHP is wonderful. echo str_replace("/$search/s", $replace, $subject); // Outputs: Hello, PHP! PHP is wonderful. ```

Understanding the Regular Expression

The regular expression used here, /$search/s, might seem a bit mysterious. Let's break it down:

Block Offensive Words on Chat in PHP
Block Offensive Words on Chat in PHP
  • $search: This is the search string you want to replace.
  • /: This is a delimiter. It can be any character, but '/' is the most common.
  • s: This is the modifier. 's' stands for 'global', which means 'replace all'.

Using str_replace() with Arrays

You can also use str_replace() with arrays to replace multiple strings at once. The first array element is the search string, the second is the replacement string, and so on.

How to use the PHP strlen() Function
How to use the PHP strlen() Function

Here's an example:

```php $subject = "Hello, World! World is wonderful."; $search = ["World", "wonderful"]; $replace = ["PHP", "amazing"]; echo str_replace($search, $replace, $subject); // Outputs: Hello, PHP! PHP is amazing. ```

Performance Considerations

a poster with different types of text and symbols on it, including the words crfluzz tool
a poster with different types of text and symbols on it, including the words crfluzz tool
REST API Response Codes: A Quick Reference
REST API Response Codes: A Quick Reference
REST API Methods Explained Simply – GET vs POST vs PUT vs DELETE Guide
REST API Methods Explained Simply – GET vs POST vs PUT vs DELETE Guide
Simple Edit Data Using PHP and MYSQL [Responsive Page]
Simple Edit Data Using PHP and MYSQL [Responsive Page]
REST API Cheat Sheet for Beginners
REST API Cheat Sheet for Beginners
the diagram shows how ssh works and what it is doing to do with it
the diagram shows how ssh works and what it is doing to do with it
Sort An Array PHP (With Example Programs)
Sort An Array PHP (With Example Programs)
owasp top 10 web application vulnerabilities
owasp top 10 web application vulnerabilities
push operation
push operation
Ahmad Awais (@MrAhmadAwais) on X
Ahmad Awais (@MrAhmadAwais) on X
what is rest api? real life example
what is rest api? real life example
40 Killer Cheat Sheets for WordPress Developers
40 Killer Cheat Sheets for WordPress Developers
a table with two different types of text on it and the words ssh pentesting
a table with two different types of text on it and the words ssh pentesting
Python Programming
Python Programming
an image of a screen with the words csrf protection on it and green text
an image of a screen with the words csrf protection on it and green text
the flow diagram shows how to use react router
the flow diagram shows how to use react router
a poster with instructions on how to use css
a poster with instructions on how to use css
Серия плакатов о реле и Arduino
Серия плакатов о реле и Arduino
FTPS vs SFTP
FTPS vs SFTP
Node-RED Context erklärt: Werte speichern ohne Datenbank
Node-RED Context erklärt: Werte speichern ohne Datenbank

While str_replace() is generally fast, it can become a performance bottleneck if used excessively or with large strings. If you're working with large strings or performing many replacements, consider using a more efficient method, such as preg_replace() with the 's' modifier, or a custom function using strpos() and substr_replace().

Conclusion

In this article, we've explored how to use PHP's str_replace() function to replace all occurrences of a string. We've covered the basic syntax, using regular expressions for global replacement, and using str_replace() with arrays. We've also discussed performance considerations to help you make the most of this powerful function.