PHP string functions programs | str_replace functions in php | how to find and replace string in php

Dear Friend, In this article we will discuss about str_replace function in PHP which is most popular and useful string functions.

what is str_replace function ?

The str_replace() is a built-in function in PHP and is used to replace all the occurrences of the search string by replacement string. Search and replace string can be both type mean: normal as single word or sentence and a collection of string as array format for search and replace.

Syntax

str_replace ( $searchValue, $replaceValue, $subjectValue, $count )

Parameters: This function accepts four parameters out of which 3 are mandatory and 1 is optional. All of these parameters are described below:

$searchValue: This parameter can be of both string and array types. This parameter specifies the string to be searched.
$replaceValue: This parameter can be of both string and array types. This parameter specifies the string with which we want to replace the $searchValue string.
$subjectValue: This parameter specifies the string which we want to search for $searchValue and replace with $replaceValue.
$count: This parameter is optional and if passed, it’s value will be set to the total number of replacement operations performed on the string $subjectValue.

Let’s understand by an example

Example 1.

Note: in this example: search, replace and subject are a string

<?php
$str_1 = "I am beginner in dynamic website development";

$after_replace = str_replace("beginner","expert",$str_1);

echo "<b>Original String</b> : ".$str_1;
echo "<br><br><b>After Replaced</b> : ".$after_replace;
?>
str_replace function
str_replace function

Logic Explanation

  • str_replace function take 1st parameter of search text here search text is “beginner
  • 2nd parameter: is replace text mean: what text should come on place of “beginner” here: search text is “expert
  • 3rd parameter: in which sentence need to perform above action, mean subject where need to find and replace the things. here “I am beginner in dynamic website development” in $str_1 variable.
  • Final string stored in $after_replace variable with expect result set.

Aslo, Read

Example 2.

Note: in this example: search and replace are type of array of string.

<?php
$str_1 = "I am beginner in dynamic website development";

$to_find_str_arr = ['beginner','website'];

$to_replace_str_arr = ['expert','Application'];

$after_replace = str_replace($to_find_str_arr,$to_replace_str_arr,$str_1);

echo "<b>Original String</b> : ".$str_1;
echo "<br><br><b>After Replaced</b> : ".$after_replace;
?>
str_replace function find in bulk and replace in bulk
str_replace function find in bulk and replace in bulk

Logic Explanation

  • str_replace function take 1st parameter of search text here search text is an array name of array is $to_find_str_arr which have 2 values “beginner” and “website
  • 2nd parameter: is replace text here replace text as an array which have 2 values “expert” and “Application“: will come on place of “beginner” and “website” respectively.
  • 3rd parameter: in which sentence need to perform above action, mean subject where need to find and replace the things. here “I am beginner in dynamic website development” in $str_1 variable.
  • Final string stored in $after_replace variable with expect result set.

Example 3.

<?php
$search_string = array('a', 'p');
$replace_fruit   = array('apple', 'pear');
$subject    = 'a p';
$final_string  = str_replace($search_string, $replace_fruit, $subject);

echo "Original String: <b>".$subject."</b>";
echo "<br/>After Replace: <b>".$final_string."</b>";

?>
str_replace function find in bulk and replace in bulk
str_replace function find in bulk and replace in bulk

Note: here subject is a b and first find a in subject and replaced with apple. Now subject become apple p. after that str_replace function start searching p and found pp in apple and p separately that mean 3 p found in subject. And replaced it with pear in these 3 place. Now 1st text will be apearpearle and 2nd text will be pear. So final text will apearpearle pear

Logic Explanation

  • str_replace function take 1st parameter of search text here search text is an array name of array is $search_string which have 2 values “a” and “p
  • 2nd parameter: is replace text here replace text as an array name is $replace_fruit which have 2 values “apple” and “pear“: will come on place of “a” and “p” respectively.
  • 3rd parameter: in which sentence need to perform above action, mean subject where need to find and replace the things. here “a p” in $subject variable.
  • Final string stored in $final_string variable with expect result set.

Example 4.

<?php
$search_string = array('a', 'p');
$replace_fruit   = array('apple', 'pear');
$subject    = 'a p';
$final_string  = str_replace($search_string, $replace_fruit, $subject,$counter);

echo "Original String: <b>".$subject."</b>";
echo "<br/>After Replace: <b>".$final_string."</b>";
echo "<br>Replace counter: <b> $counter </b>";

?>
str_replace with counter variable
str_replace with counter variable

Note: this is an extended version of example 3, with additional attributes of str_replace function which is $counter, counter will be 4 because first replace a with apple and 2nd replace 3p with pear, so total counter will be 4

Watch Video

Conclusion

Dear Friend, I hope this will help you. Let me know, if you have any doubt and query related to this in below comment section. Thanks.

Pradip Mehta

I am a well-organized professional in Drupal Development and PHP web development with strong script handling knowledge or automation process with PHP. I have advanced computer skills and am a proficient multitasker.