How to replace occurrences of a word or phrase in PHP string
  John Mwaniki /   13 Dec 2021

How to replace occurrences of a word or phrase in PHP string

It can be an overwhelming task if you were given a huge string of text and required to manually replace all the instances of a certain word or phrase in it with a different one. Let's say for instance in a 2,000 words blog article with a word appearing multiple times at varying positions throughout the article.

The good thing is that most programming languages have inbuilt functions that allow us to do that programmatically. In this article, we will cover different ways in which you can replace a certain character, word, or phrase in a PHP string.

1. Using the str_replace() function

The str_replace() function is an inbuilt PHP function used to replace all the occurrences of some characters with other characters in a string.

Syntax

str_replace(find,replace,string,count)

Parameter Requirement Description
find Required Specifies the character, word or, phrase to find.
replace Required Specifies the character, word or, phrase to replace the value in find with.
string Required This is the actual string that contains the characters or phrases to be replaced.
count Optional This parameter is a variable that contains the number of replacements that have happened after the function has been executed.

Example 1

<?php
$str = "Hello World!";
echo str_replace("World", "Joseph", $str);

Output:

Hello Joseph!

In our example above, $str is our string that contains the word we wanted to replace. The word "World" is what we were finding and replacing the word "Joseph".

If the string was bigger, and with more occurrences of the word "World", it would have been replaced with the word "Joseph" everywhere throughout the string like in the example below.

Example 2

<?php
$str = "Hello World! Why always World?";
echo str_replace("World", "Joseph", $str);

Output:

Hello Joseph! Why always Joseph?

As you can see, all the occurrences of "World" have been replaced.

Example 3

Let's now include the fourth parameter.

<?php
$str = "Hello World! Why always World?";
echo str_replace("World", "Joseph", $str, $total);
echo "<br>";
echo "We made $total replacements";

Output:

Hello Joseph! Why always Joseph?
We made 2 replacements

Here we have passed the variable $total as our fourth parameter to store the value of a total number of replacements that happen. In this case, 2 replacements have happened.

Replacing multiple words at the same time

You may also want to replace several phrases within a string at a go, each with a different phrase. To do this, you need to create an array with a list of the phrases to replace and another array with a list of all the phrases you want to replace them with respectively.

The first array acts as the first parameter and the second one as the second parameter of the str_replace() function respectively.

Example

<?php
$str = "My name is John. I love programming. I like programming in PHP.";
$find = array("John","programming","PHP");
$replace = array("Nicholas","coding","JavaScript");
echo str_replace($find, $replace, $str);

Output:

My name is Nicholas. I love coding. I like coding in JavaScript.

The words John, programming, and PHP have been replaced with the words Nicholas, coding, and JavaScript respectively at a go.

Note: The PHP str_replace() function is case-sensitive. If you want to perform a case-insensitive find and replace, use the str_ireplace() function in its place.

2. Using the preg_replace() function

The preg_replace() function replaces all matches of a pattern or list of patterns within a string with substrings.

Syntax

preg_replace(pattern, replacement, input, limit, count)

Parameter Requirement Description
pattern Required Consists of a regular expression or an array of regular expressions.
replacement Required This consists of a replacement string or an array of replacement strings.
input Required This is the string or array of strings in which replacements are to be done.
limit Optional This sets a limit on how many replacements should be done in each string.
count Optional This parameter is a variable that contains the number of replacements that have happened after the function has been executed.

The pattern comprises of the word or phrase to be replaced enclosed within an opening and closing forward slash /.

Example

<?php
$str = "My name is John. I love programming. I like programming in PHP.";
echo preg_replace("/programming/", "coding", $str);

Output:

My name is John. I love coding. I like coding in PHP.

We wanted to replace all the occurrences of the word "programming" with the word "coding". So in we have made a pattern by enclosing the word "programming" within forward slash as "/programming/" and then passed "coding" to the function as the second argument.

However, this type of pattern is case-sensitive and will not replace words or phrases that don't match the same case as the pattern.

Example

<?php
$str = "My name is John. I love Programming. I like programming in PHP.";
echo preg_replace("/programming/", "coding", $str);

Output:

My name is John. I love Programming. I like coding in PHP.

In our string, the first word "Programming" started with an uppercase P, and thus it was not replaced in the resulting string.

To make this replacement case-insensitive, all we have to do is add an "i" after the closing forward slash.

Example

<?php
$str = "My name is John. I love Programming. I like programming in PHP.";
echo preg_replace("/programming/i", "coding", $str);

Output:

My name is John. I love coding. I like coding in PHP.

Replacing multiple words at the same time with preg_replace()

In a similar way to str_replace(), you will need to create an array with a list of the patterns to replace and another array with a list of all the phrases you want to replace them with respectively.

Example

<?php
$str = "My name is John. I love programming. I like programming in PHP.";
$patterns = array("/John/i","/programming/i","/PHP/i");
$replacements = array("Nicholas","coding","JavaScript");
echo preg_replace($patterns, $replacements, $str);

Output:

My name is Nicholas. I love coding. I like coding in JavaScript.

That's it!

By following this guide you can comfortably replace some characters, words, or phrases within a PHP string.