In many applications, there is a need to search and replace the text with something else.
PHP offers the built-in function to replace a string. We use the str_replace function in PHP to replace the string occurrences in search or array string search. This inbuilt function is used to replace all the word occurrences within a string.
This article will discuss how we can use the str_replace function in PHP to replace a string. There are example code snippets along with the output for better understanding.
Table of Contents
str_replace( ) :
str_replace( ) is a builtin function of PHP. The syntax of the PHP str_replace ( ) function is given by:
str_replace( $search, $replace, $subject, $count )
The following four parameters are used in this function. Three of them are mandatory, and one is optional to use.
- $search: It will contain a search string that will be searched for replacing.
- $replace: It will specify the string that will be replacing the original string.
- $subject: it defines the string or array from where the string is searched in $search and then string replacement with $replace.
- $count: This is an optional parameter. It has a count of how many replacement operations have been done on the string in $subject. This parameter accepts a number.
Search and Replace; both parameters can have either a string or array of strings.
Look at the following examples for a clear understanding of the PHP str_replace function.
Example Code. 01:
This is one simple example to learn PHP str_replace function.
<?php
$subject = "Ross watched TV last night.";
$search = "Ross";
$replace = "Maria";
$mysentence= str_replace($search, $replace, $subject);
echo $mysentence;
?>
Output:

Example Code. 02:
The following example uses an array with the PHP replace function.
<?PHP
$subject = "I bought one oval, antique, coffee table.";
$search = array("oval", "antique", "coffee");
$replace = array("round", "modern", "dining");
$mysentence = str_replace($search, $replace, $subject);
print_r($mysentence);
?>
Output:

Example Code. 03:
The count parameter is optional in PHP String function. The below example shows how this parameter works in PHP str_replace function.
<?php
$subject = "Brenda had 3 times as many dresses as Cathy. After Brenda gave away 75 dresses, Brenda had half as many dresses as Cathy. ";
$search = "Brenda";
$replace = "Rachel";
$count = 0;
$mysentence = str_replace($search, $replace, $subject, $count);
echo $mysentence;
echo $count;
?>
Output:

Example Code. 04:
If we want to replace multiple words simultaneously, it will be done like the following example.
<?php
$subject = "Marilyn saves 30% of the money she earns each month. She earns $350 each month. She is spending one third on each week.";
$search = ["month", "week"];
$replace = "year";
$mysentence = str_replace($search, $replace, $subject);
echo $mysentence;
?>
Output:

Example Code. 05:
We can remove a string with the help of the following method.
<?PHP
$subject = "Although Joe, Monica and Ross broke their arm, Alex still cheered for their team from the sidelines.";
$search = [", Monica"];
$replace = "";
$mysentence = str_replace($search, $replace, $subject);
echo $mysentence;
?>
Output:

Conclusion:
The article has explained different example codes to understand how to replace a string in PHP. The codes were simple enough to understand the PHP str_replace function. I hope this piece of information helped you in your development.