How to Use the PHP STR_REPLACE Function to Find and Replace Strings
If you’ve ever needed to find and replace a string in PHP, then you’ve needed PHP STR_REPLACE. The PHP STR_REPLACE function works to replace each instance of a given string with another string.
And, luckily for you, it’s also one of the easiest functions to use. Today, we’re going to take a look at how you use PHP STR_REPLACE, what situations you would use it in, and what you shouldn’t do when using it.
What is PHP STR_REPLACE?
PHP STR_REPLACE stands for “string replace.” This function in PHP searches for a given string within another string. Wherever it finds that string, it replaces it.
The STR_REPLACE function can work with a string or an array. It will return a string if used with a string, and it will return an array if used with an array.
How do you use PHP STR_REPLACE?
In the PHP manual, PHP STR_REPLACE is used with the following syntax:
str_replace($search_string, $replace_string, $original_string, [$count]);
First, let’s explain the variables:
- $search_string: The string that you’re searching for.
- $replace_string: What you want to replace $search_string with.
- $original_string: The string that’s being searched (or an array of strings).
- $count: Optionally, you can include a variable that will count the number of replacements made.
Let’s look at a simple example:
$string_world = "Hello, world!"; $string_everyone = str_replace("world","Everyone",$string_world); echo $string_world; echo "<br>"; echo $string_everyone;
In the above example, we started with $string_world. Then, the replacement swaps “world” with “everyone.” The PHP strings within the string are swapped. Note that this is case-sensitive. If we had written “WORLD” instead of “world,” it would not have matched any strings.
Additionally, we did not use the “$count” variable in this example because it’s an optional variable.
Last Updated October 2022
PHP for Beginners: learn everything you need to become a professional PHP developer with practical exercises & projects. | By Edwin Diaz, Coding Faculty SolutionsExplore Course
Using PHP STR_REPLACE with an empty string
You can also use a replacement function with empty PHP strings. It basically deletes the given string when you do this: it inserts a null value.
In the below example, instead of replacing “world” with “Everyone,” we replace “world” with “” (a null set).
Now we’re saying hello to no one. So, STR_REPLACE could strip out extraneous things that you don’t want in your code or things that could potentially damage your code. For instance, you could use STR_REPLACE to strip out quotes (“s), or you could use it to strip out tags (<>s).
Using PHP STR_REPLACE with multiple replacements
We can also replace multiple iterations of a string. In the above example, we were just trying to replace the word “world.” But STR_REPLACE will replace as many instances of the string as it can find.
Let’s say that, for some reason, we wanted to turn every “L” in the phrase to a “7” (maybe we’re generating a password). We could do this just by changing “world” to “l” and “Everyone” to “7.”
So, it’s important to note that STR_REPLACE will always replace every instance within the string. There is no way to get it to replace only certain instances of the string or a certain number of instances — you’d need to use another function for that.
Top courses in PHP
Using PHP STR_REPLACE without case sensitivity
PHP is only partially case-sensitive, which can be confusing. The STR_REPLACE function works exactly as STR_REPLACE does but without case sensitivity. So, if we wanted to replace “world,” “WORLD,” “World,” or any other variations of the above, we would use STR_IREPLACE instead.
In every other aspect, STR_IREPLACE will operate the same.
Finding out how many replacements were made
As mentioned, we have an optional “count” variable that we can use to determine how many replacements occurred. We do this by providing a variable (in this case, $count) and then calling that variable after the function has finished.
Because PHP doesn’t enforce strict variable controls, you don’t need to declare the $count variable before you call STR_REPLACE. But it’s generally considered a best practice to do so.
You may realize something else. If we didn’t want to perform a substr replace, we could actually use this only to determine the number of instances within the string. Check out this example:
In the above example, we don’t replace anything at all. But we do get a count of the Ls in the sentence.
Of course, this isn’t the most effective way to do this. You could do this with SUBSTR_COUNT() or even PHP FOREACH. But it does help understand how the function works.
Arrays and PHP STR_REPLACE
In addition to performing a substr replace, PHP STR_REPLACE can also look through an array and replace instances of a string within every string inside of that array. It works exactly the same as a traditional STR_REPLACE, just across all the strings of the array itself.
Let’s start with an array called $array_greetings that we iterate through with the PHP FOREACH function:
Now, let’s say we want to change that to “Goodnight” instead of “Hello.”
Voila! We’ve performed a SUBSTR replace on the array with all occurrences replaced, even though it’s an entire array. So, the function returns a string when you give it a string, but it will return an array when you give it an array.
If we had a whole bunch of strings that we wanted to change, we could pull them into an array stack and change them all at once.
Learning more about PHP STR_REPLACE
There are other options, such as PREG_REPLACE, that can insert a replacement string — although PREG_REPLACE, in particular, tends to be a little more complicated than STR_REPLACE.
Regardless, if you need to search for a string and replace it, as well as report the occurrences of search, the PHP STR_REPLACE function is perfect. It’s a simple, easy function that you can use to swap one string for another inside of an original string or an array.