Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
Advertisement
  1. Code
  2. PHP

How to Replace Strings in PHP

by
Difficulty:BeginnerLength:ShortLanguages:

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.

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.

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.

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.

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.

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.

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:

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.

Advertisement
Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.