php

PHP Str_Replace() Function

The str_replace() is the built-in function of PHP to search and replace all occurrences or the particular number of occurrences of the string. It is a very useful function to replace the content of a string or the content of the file based on the search string. This function searches and replaces the string in a case-sensitive manner. The different ways of using the str_replace() function are shown in this tutorial.

Syntax:
str_replace ( $search_value, $replace_value, $subject_value, $count )

This function can take four arguments. The first three arguments are mandatory arguments and the fourth argument is optional. The purposes of the arguments of this function are described in the following:

  1. $search_value

    This argument can be a string or an array. The string or array is used to search a string or the array values in the main string.

  2. $replace_value
    This argument can be a string or an array. The string or array is used to replace a string data in the main string based on the search string or array.
  3. $subject_value

    This argument is a string where the $search_value is searched and replaced by $replace-value.

  4. $count

    This argument is optional and is used to count the total number of replacements.

Different Examples of Str_Replace() Function

The different uses of the str_replace() function are shown in this part of the tutorial using multiple examples.

Example 1: Replace a String Based on a Search Word and Replace the Word

Create a PHP file with the following script that searches for a word in a string. If the word exists in the string, the word is replaced by another word. According to the script, the word ‘”PHP” is searched and replaced by the word “Java” using the str_replace() function.

<?php

//Assign a string value
$stringVal = "PHP Programming";
echo "The original string: <b>$stringVal</b><br/>";
//Replace the string based on the search string
echo "The modified string: <b>". str_replace("PHP", "Java", $stringVal)."</b><br/>";

?>

Output:

The following output appears after executing the previous script. Here, the word “PHP” exists in the main string and it is replaced by the word “Java”.

Example 2: Replace a String Based on the Search Array and Replace It with a Word

Create a PHP file with the following script that searches the values of an array inside a string and replace it with a word where the value matches. According to the script, the search array contains two values that match with two words of the main string. So, these two words of the main string are replaced by the “defined replace a word” using the str_replace() function.

<?php

//Assign string value
$stringVal = "I like Pasta and I love Pizza";

//Define the search array
$search = array("like", "love");
//Define the replacement string
$replace = "eat";

echo "The original string: <b>$stringVal</b><br/>";
//Replace the string based on the search string
echo "The modified string: <b>". str_replace($search, $replace, $stringVal)."</b><br/>";

?>

Output:

The following output appears after executing the previous script. Here, two words – “like” and “love” – are replaced by the word “eat”:

Example 3: Search and Replace the Words Using the Search and Replace Arrays

Create a PHP file with the following script that searches the values of an array inside a string and replace them with the values of a replacement array where the value matches. According to the script, the search and replace arrays contain the same number of elements. Here, all elements of the search array exist in the main string. So, all matching words of the main string are replaced by the corresponding words of the replacement array using the str_replace() function.

<?php

//Assign string value
$stringVal = "I like Chicken but I don't like Fish";

//Define the search array
$search = array("Chicken", "Fish", "like");
//Define the replace array
$replace = array("Ice-cream", "Soup", "eat");

echo "The original string: <b>$stringVal</b><br/>";
//Replace the string based on the search string
echo "The modified string: <b>". str_replace($search, $replace, $stringVal)."</b><br/>";

?>

Output:

The following output appears after executing the previous script. Here, the word “like” is replaced by the word “eat” two times. The other two words, “Chicken” and “Fish”, are replaced by the words “Ice-cream” and “Soup” in the main string:

Example 4: Count the Total Number of Replacement

Create a PHP file with the following script searches the values of an array inside a string and is replaced by the values of a replace array where the value matches like the previous example and counts the total number of replacement. The fourth argument of the str_replace() function is used here to count the total number of replacements. According to the script, the search and replace arrays contain the same number of elements. Here, four words of the main string match with the values of the search array which is replaced by the corresponding words of replace array using the str_replace() function.

<?php

//Assign string value
$stringVal = "I like Chicken but I don't like Fish";

//Define the search array
$search = array("Chicken", "Cake", "like");
//Define the replace array
$replace = array("Ice-cream", "Soup", "eat");

echo "The original string: <b>$stringVal</b><br/>";
//Replace the string based on the search string
echo "The modified string: <b>". str_replace($search, $replace, $stringVal, $count)."</b><br/>";
echo "Total number of replacements: <b> $count </b>";

?>

Output:

The following output appears after executing the previous script. Here, the word “like” is replaced by the word “eat” two times. The other word, “Chicken”, is replaced by the word “Ice-cream” in the main string. So, the total number of replacements is 2+1 = 3:

Conclusion

The different ways of using the str_replace() function are shown in this tutorial using multiple examples. The search and replace tasks are done using the search word or array and replace word or array. We hope that the purpose of using the str_replace() function in the PHP script is cleared after reading this tutorial.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.