4 Ways To Replace Strings In PHP

Did you know there is more than one way to replace a string within a string? Well, in this article, we will dive into each of the different, native functions that PHP gives you for this specific use-case.

 

str_replace

String replacement with str_replace()

Probably the most common native function to use for string replace is str_replace(), if you haven’t used it before, you’re going to love it. It’s super simple and super-quick!

str_replace() takes three main parameters with an extra optional parameter to get the number of replacement occurrences. The 3 parameters can be in the shape of a singular string or an array of strings, meaning you can replace multiple phrases in one call to the function.

The main parameters are as follows and as ordered –

  1. Search term
  2. Replacement term
  3. Subject/String to be replaced

Quick Note: str_replace is Case Sensitive.

Replacing a single value from a string

PHP

$myString = "This is my super-massive boring string."; // Subject - String to be replaced
$mySearch = "super-massive"; // The phrase that we want to search inside $myString.
$myReplacement = 'tiny'; // The phrase we want to replace the searched & matched phrase with.

echo str_replace($mySearch, $myReplacement, $myString);

Output

This is my tiny boring string.

As you can see from the code and output above, the word super-massive was replaced with the word ‘tiny’.

Replacing multiple characters in a string

With multiple replacements, we can pass in arrays instead of singular values. Notice that you can mix these up, having one array of search values and one singular replacement value. Or, alternatively, if you have two arrays with multiple values, it will sequentially replace them.

PHP

$myString = "This is my super-massive boring string."; // Subject - String to be replaced
$mySearchArray = ["super-massive", "boring"]; // The array of phrases that we want to search inside $myString.
$myReplacement = 'tiny'; // The phrase we want to replace the searched & matched phrase with.


echo str_replace($mySearchArray, $myReplacement, $myString);


// Or

$myString = "This is my super-massive boring string."; // Subject - String to be replaced
$mySearchArray = ["super-massive", "boring", 'string']; // The array of phrases that we want to search inside $myString.
$myReplacementArray = ['tiny', 'awesome', 'stringy string']; // The array of phrases we want to replace the searched & matched phrase with.

echo str_replace($mySearchArray, $myReplacementArray, $myString);

Output

This is my tiny tiny string.

This is my tiny awesome stringy string.

str_ireplace

Although extremely similar, the str_ireplace function works a tad differently. The slight difference is that the function doesn’t care about case, as its completely case-insensitive.

Apart from the case insensitivity, the function is exactly the same.

The main parameters are as follows and as ordered –

  1. Search term
  2. Replacement term
  3. Subject/String to be replaced
Replacing a single value from a string case-insensitive

PHP

$myString = "ThiS iS mY Weird StriNg!"; // Subject - String to be replaced
$mySearch = "WEIRD"; // The case insensitive string to search
$myReplacement = "COOL"; // The phrase we want to replace the searched & matched phrase with.


echo str_ireplace($mySearchArray, $myReplacement, $myString);

Output

ThiS iS mY COOL StriNg!
Replacing multiple values in a string case-insensitive

Again with this replace function, it allows the usage of arrays and therefore we can pass in our array of values to search and replace like the following –

PHP

$myString = "ThiS iS mY Weird StriNg!"; // Subject - String to be replaced
$mySearchArray = ["WEIRD", "my"]; // The case insensitive string to search
$myReplacement = ['COOL', "YOUR"]; // The phrase we want to replace the searched & matched phrase with.


echo str_replace($mySearchArray, $myReplacement, $myString);

Output

ThiS iS YOUR COOL StriNg!

 

substr_replace

Now, substr_replace gives us the ability to be ultra-specific when replacing text within a string. It allows us to tell PHP to only replace within a specific location of a string, for example, the first ten letters. What this function doesn’t have though, is the search capability that str_replace and str_ireplace have.

The parameters of the function are as follows –

  • Subject/String to be replaced
  • Replacement phrase
  • Start index, for example, 0 will start from the beginning of the string.
  • Length int, the number of characters along the string.

So, let’s put this into some code

PHP

$myString = "Mr Coder, How are you today?"; // Subject - String to be replaced
$myReplacement = 'Dr'; // The phrase we want to replace the searched & matched phrase with.


echo substr_replace($myString, $myReplacement, 0, 2); // Start replacement from the beginning of string, 2 characters along.

Output

Dr Coder, How are you today?

preg_replace

If you want to replace with a bit more complexity, then PHP has the preg_replace function. This allows you to pass in a regular expression to be matched and replaced within a given string.

Its parameters are similar to the above functions, but rather than an explicit search term, a regular expression is in its place.

Parameters

  1. Regular Expression
  2. Replacement
  3. String/subject to be replaced
Singular regular expression replacement

PHP

php regex replacement example

Output

MrCoder,Howareyoutoday?

 

Functional References

Check out the documentation for the functions used in this tutorial below –

As the saying goes, there is more than one way to skin a cat, well, in PHP, there is more than one way to replace a string!.

Posted in PHP

Leave a Reply