If you have ever searched for “string replace in php“, you are usually trying to do one simple thing: take some text and swap part of it with something else. In PHP, string replacement comes up all the time in real projects, from cleaning user input to templating, formatting emails, or updating URLs. In this guide, I will walk through the main tools you can use for string replace in PHP, show short examples, and help you decide which function makes the most sense in different situations.
TL;DR
- Use
str_replace()for most “find and replace” string operations in PHP. - Use
str_ireplace()when you need case-insensitive string replace in PHP. - Use
preg_replace()when you need patterns or regular expressions, not just plain text. - Strings in PHP are immutable. Every string replace in PHP returns a new string, the original string does not change.
- For performance, reuse patterns and avoid unnecessary preg_replace() calls when str_replace() is enough.
What does string replace in PHP actually do?
At the core, a string replace in PHP takes three things:
- The text you want to search for.
- The replacement text you want to use instead.
- The original string you want to modify.
Then PHP returns a new string where every occurrence of the search text has been swapped with your replacement.
The important part is that the original string is not changed. PHP returns a new string value, so you usually assign it to a variable.
Basic string replace in PHP with str_replace()
The most common and easiest function for string replace in PHP is str_replace().
$original = "I love JavaScript.";
$search = "JavaScript";
$replace = "PHP";
$result = str_replace($search, $replace, $original);
// $result: "I love PHP."
// $original is still "I love JavaScript."This is handy when you are debugging or when you want to track how often something appears. Keep in mind that this function is case-sensitive. For example, in the code above, if javaScript were the value of the $search variable, then no match would’ve been found, and the string would not have been replaced.
Case-insensitive string replace in PHP: str_ireplace()
Sometimes you do not care about case, you want to replace a word, no matter how it is capitalized.
That is when you use str_ireplace().
$text = "Php is great, I love php!";
$search = "php";
$replace = "PHP";
$result = str_ireplace($search, $replace, $text);
// $result: "PHP is great, I love PHP!"Key difference:
str_replace()is case sensitive.str_ireplace()is case insensitive.
Replacing multiple strings at once
One nice feature of str_replace() and str_ireplace() is that they accept arrays for both the search and replacement values. This lets you perform multiple string replacements in a single call.
$text = "Hello NAME, welcome to CITY!";
$search = ["NAME", "CITY"];
$replace = ["Chris", "Birmingham"];
$result = str_replace($search, $replace, $text);
// "Hello Chris, welcome to Birmingham!"A few rules to remember:
- PHP walks both arrays by index.
search[0]is replaced byreplace[0],search[1]byreplace[1], and so on.- If the replacement array has fewer items, missing ones are treated as empty strings.
This pattern is helpful for simple templating, email content, or system notifications that use placeholder tags to be replaced.
When do you need preg_replace() for string replace in PHP?
str_replace() only works with literal strings. If you need to replace patterns, like “all numbers” or “anything inside square brackets”, you need regular expressions, and that is where preg_replace() comes in.
$text = "Order #12345 placed on 2025-12-09.";
$result = preg_replace('/\d+/', 'BBB', $text);
// "Order #BBB placed on BBB."In this example:
/\d+/is a regular expression that matches “one or more digits”.preg_replace()scans the string and replaces each match with “BBB”.
You would reach for preg_replace() when:
- You need to clean up user input (emails, phone numbers, IDs).
- You want to remove or replace HTML tags.
- You need to normalize formats, like dates or IDs, where a fixed search string is not enough.
Just remember that regular expressions are more powerful, but also slower and harder to read. If a simple string replace in PHP solves your problem, stick with str_replace() or str_ireplace().
What about performance when I replace a string in PHP?
In most real projects, performance does matter, especially when you are doing string replace inside loops or over large buffers.
Some simple tips:
- Use
str_replace()instead ofpreg_replace()when you do not need patterns. - If you are replacing many different values, use the array form of
str_replace()instead of calling it over and over in a loop. - Avoid performing string replacements on very large strings repeatedly in a row. Combine your replacements when it makes sense.
Example of a more efficient approach:
// Less efficient
$text = some_big_string();
$text = str_replace('foo', 'bar', $text);
$text = str_replace('hello', 'hi', $text);
$text = str_replace('world', 'earth', $text);
// More efficient
$text = some_big_string();
$text = str_replace(
['foo', 'hello', 'world'], // Search array
['bar', 'hi', 'earth'], // Replace array
$text
);The second version walks the string only once, instead of three times.
Common mistakes with string replace in PHP
Here are a few things that trip people up when working with string replace in PHP.
1. Forgetting the result is a new string
PHP does not modify the original string variable in place. If you do not assign the result, the replacement might as well never have happened.
$name = "Christopher";
str_replace("Chris", "Topher", $name);
// $name is still "Christopher"You need:
$name = str_replace("Chris", "Topher", $name);2. Using preg_replace() when str_replace() is enough
Using a regular expression when you do not need one:
- Makes the code harder to read.
- Can be slower without any real benefit.
Always start with str_replace(). Switch to preg_replace() only when you need pattern matching.
3. Ignoring encoding and multibyte issues
Like most string functions in PHP, str_replace() operates on bytes, not characters. In standard English text, you usually do not feel this. For multibyte encodings like UTF-8, if you are doing more complex work, you might need the mb_* functions or regular expressions with the u (Unicode) modifier.
Which string replace function should I use in PHP?
Here is a simple rule of thumb that I use:
- Use
str_replace()when you know the exact text you want to replace and case matters. - Use
str_ireplace()when you know the exact text, but case does not matter. - Use
preg_replace()when you need pattern matching, variable formats, or complex rules.
If you follow that, you will cover almost every real-world “string replace in PHP” situation without overcomplicating your code.








