Finding and replacing one piece of text with something else is a very common task in a lot of applications. It is also needed regularly when we are working with text in PHP. Therefore, this tutorial will focus on replacing one string with another in PHP.
Replace Strings in PHP Using str_replace()
Like a lot of things, PHP also has a built-in function that you can use to replace strings in PHP. The str_replace($search, $replace, $subject, $count) function takes four parameters that determine how the function will behave.
The first parameter is $search. It's used to specify the strings that you are searching for to replace. The strings which will replace the original string are specified using $replace. The $subject parameter is the main string or haystack within which you will do all your replacements. The value of both $search and $replace can either be an array or a string.
Replace One String With Another in PHP
We can simply provide a string value for $search and $replace when we only want to replace a single string with something else.
1 |
<?php
|
2 |
|
3 |
$sentence = "Andrew prefers tea to coffee."; |
4 |
|
5 |
$search = "Andrew"; |
6 |
$replace = "Adam"; |
7 |
|
8 |
$new_sentence = str_replace($search, $replace, $sentence); |
9 |
|
10 |
// Adam prefers tea to coffee.
|
11 |
echo $new_sentence; |
12 |
|
13 |
?>
|
Replace Multiple Strings With a Single String
As I mentioned earlier, you can specify the value of $search to be an array. If $replace is a single string, then all the values in $search array will be replaced by $replace in our main string.
1 |
<?php
|
2 |
|
3 |
$sentence = "There were 60 bananas and 12 oranges in the first basket. Some children ate 2 mangoes each."; |
4 |
|
5 |
$search = ["bananas", "mangoes"]; |
6 |
$replace = "apples"; |
7 |
|
8 |
$new_sentence = str_replace($search, $replace, $sentence); |
9 |
|
10 |
// There were 60 apples and 12 oranges in the first basket. Some children ate 2 apples each.
|
11 |
echo $new_sentence; |
12 |
|
13 |
?>
|
Remove Characters or Substrings From a String
Removing characters or substrings from a string is simply a matter of setting the value of $replace to an empty string.
1 |
<?php
|
2 |
|
3 |
$sentence = "Andrew, Monty and Adam will be meeting at the club today."; |
4 |
|
5 |
$search = [", Monty"]; |
6 |
$replace = ""; |
7 |
|
8 |
$new_sentence = str_replace($search, $replace, $sentence); |
9 |
|
10 |
// Andrew and Adam will be meeting at the club today.
|
11 |
echo $new_sentence; |
12 |
|
13 |
?>
|
Replace Some Substrings and Remove Others From a String
Both $search and $replace can be specified as an array. In this case, the first element in $search will be replaced by the first element in $replace and so on. In other words, substrings in $search will be replaced by substrings in $replace at corresponding indices.
What if the $search and $replace arrays have unequal length? If $search has more elements than $replace, then extra elements in $search will be replaced by an empty string.
1 |
<?php
|
2 |
|
3 |
$sentence = "There were 60 bananas and 12 oranges in the first basket. Some children ate 2 mangoes each."; |
4 |
|
5 |
$search = ["bananas", "mangoes"]; |
6 |
$replace = ["apples"]; |
7 |
|
8 |
$new_sentence = str_replace($search, $replace, $sentence); |
9 |
|
10 |
// There were 60 apples and 12 oranges in the first basket. Some children ate 2 each.
|
11 |
echo $new_sentence; |
12 |
|
13 |
?>
|
This example is very similar to the one we wrote above to replace bananas and mangoes with apples. The only difference this time is that $replace is now an array with a single element. However, this results in a completely different outcome because mangoes will be replaced by an empty string now.
Counting the Number of Replacements
There is a fourth parameter that you can pass to str_replace(). The value of this fourth parameter is set to the number of replacements performed by str_replace(). It can be helpful in a variety of situations. For example, if you know beforehand how many replacements should be performed by str_replace(), then you can use the value set by str_replace() to verify that there were no unexpected replacements.
1 |
<?php
|
2 |
|
3 |
$sentence = "The house was filled with apples. There were apples in the fridge and there were apples on the dining table. |
4 |
There were multiple baskets of apples in the living room. We were eating apples for breakfast, lunch and dinner."; |
5 |
|
6 |
$search = "apples"; |
7 |
$replace = "mangoes"; |
8 |
$count = 0; |
9 |
|
10 |
$new_sentence = str_replace($search, $replace, $sentence, $count); |
11 |
|
12 |
// The house was filled with mangoes. There were mangoes in the fridge and there were mangoes on the dining table.
|
13 |
// There were multiple baskets of mangoes in the living room. We were eating mangoes for breakfast, lunch and dinner.
|
14 |
echo $new_sentence; |
15 |
|
16 |
// 5
|
17 |
echo $count; |
18 |
|
19 |
?>
|
Important Points to Keep in Mind
Just like a lot of other functions, PHP comes with a case-insensitive version of str_replace() called str_ireplace(). It works just like str_replace() but does the replacements in a case-insensitive manner.
1 |
<?php
|
2 |
|
3 |
$sentence = "I ate one aPPle and he ate three Apples."; |
4 |
|
5 |
$search = "apple"; |
6 |
$replace = "banana"; |
7 |
|
8 |
$new_sentence = str_ireplace($search, $replace, $sentence); |
9 |
|
10 |
// I ate one banana and he ate three bananas.
|
11 |
echo $new_sentence; |
12 |
|
13 |
?>
|
When you are using str_replace(), make sure that the strings you are replacing are not part of larger words. This can result in some unexpected results. Here is an example:
1 |
<?php
|
2 |
|
3 |
$sentence = "My nana was eating a banana."; |
4 |
|
5 |
$search = "nana"; |
6 |
$replace = "friend"; |
7 |
|
8 |
$new_sentence = str_replace($search, $replace, $sentence); |
9 |
|
10 |
// My friend was eating a bafriend.
|
11 |
echo $new_sentence; |
12 |
|
13 |
?>
|
Final Thoughts
In this tutorial, we have covered different situations that might arise when we are replacing strings in PHP. The str_replace() function is ideal for any such basic replacements. However, it is important to be careful with the values that we supply to different parameters. Otherwise, we will get some unexpected results as demonstrated in the examples. You should consider using the preg_replace() function for more complicated string replacements.







