In this article, you going to see discussion regarding str_replace php in a detail and step by step with example.

If you are looking for this topic then you are at right place.

Let’s start with the topic of str_replace php

What is str_replace function in PHP?

In PHP, str_replace function is used to replace the characters with some another characters in the string.

Below are the rules on which this function works.

  • Returns an Array during searching of the string is an array.
  • Find and Replace is going to be performed for each and every element array element during searching of the string is an array.

It is a case sensitive and binary safe.

Below is syntax for str_replace function.

str_replace(find,replace,string,count)
ParameterDetails
findIt is Required. Represent the value to find
replaceIt is Required. Represent the value to replace the value to find
stringIt is Required. Represent the string to be searched.
countIt is an Optional. Count number of replacement
Syntax String Replacement

How to use str_replace in php?

Now you going to see how to replace string using str_replace function.

Below is code snippet from which you can understand.

<!doctype html><html lang="en"><head> <title>How to use str_replace in php</title></head><body><div style="background-color:#e5bcbc;" align="center"><h2>How to use str_replace in php</h2><?php$arr = array("one","two","three","four");echo 'STRING : ';print_r($arr);echo '<br/>';print_r(str_replace("two","four",$arr,$i));echo "<br>" . "Replacements: $i";?></div> </body></html>

In above code snippet you str_replace function.

  • The first parameter is to be found.
  • The second parameter is to be replaced with the first parameter.
  • The third parameter is the string in which has to find and replace characters.
  • The fourth parameter is to count the number of replacements.

OUTPUT:

How to use str_replace in php
How to use str_replace in php

How to use str_replace for multiple value replacement in PHP?

Now you going to see how to do multiple value replacement string using str_replace function.

Let’s look with the below code snippet from which you can understand.

<html lang="en"><head> <title>How to use str_replace in php</title></head><body><div style="background-color:#e5bcbc;" align="center"><h2>How to use str_replace for multiple value replacement in PHP</h2><?phpecho 'STRING : '.$subject = 'milk is white and contains sugar'.'<br/>';print_r(str_replace(array('sugar', 'milk'), array('sweet', 'white'), $subject,$i));echo "<br>" . "Replacements: $i";?></div> </body></html>

For multiple value replacement string, you have to just pass the parameter in the array format.

Pass first and second parameter both as an array see in above example.

OUTPUT :

How to use str_replace for multiple value replacement in PHP?
How to use str_replace for multiple value replacement in PHP?

How to replace special characters in PHP?

Now you going to see how to do replace special characters from string using str_replace function.

Now we going to check code given below.

<html lang="en"><head> <title>How to use str_replace in php</title></head><body><div style="background-color:#e5bcbc;" align="center"><h2>How to replace special characters in php</h2><?phpecho 'STRING : '.$subject = '$# Good is white and contains sugar'.'<br/>';echo 'OUTPUT : ';print_r(str_replace(array('sugar', '$#'), array('sweet', 'milk'), $subject,$i));echo "<br>" . "Replacements: $i";?></div> </body></html>

Using str_replace function you can replace the special character from the string.

Use code snipped for multiple value replacement.

First parameter is used to passed array containing value to be find.

Second parameter passed array to be replace with the first parameter.

And third parameter is the string in search is going to be place.

And Last Parameter is count the number of replacement.

OUTPUT :

How to replace special characters in PHP Using str_replace ?
How to replace special characters in PHP?

What is string manipulation PHP?

String manipulation in PHP is a replacement of string.

Replace of a string you can do using any of the above example.

From above example you can understand how manipulation can be done.

How can I replace substring in PHP?

Now you going to see how you can replace substring using str_replace function.

Using below example you can replace substring

<html lang="en"><head> <title>How to use str_replace in php</title></head><body><div style="background-color:#e5bcbc;" align="center"><h2>How can I replace substring in PHP</h2><?phpecho 'STRING : '.$subject = 'milk is white and contains sugar'.'<br/>';echo 'SUBSTRING : '.$subsrting = substr("milk is white and contains sugar",0,4); echo "<br>";echo 'OUTPUT : ';print_r(str_replace(array('sugar', $subsrting), array('sweet', 'Good'), $subject,$i));echo "<br>" . "Replacements: $i";?></div> </body></html>

In above code snippet used substr function to get sub string form the string.

After getting substring then replace the sub-string using str_replace function.

OUTPUT:

How can I replace substring in PHP?
How can I replace substring in PHP?

How to insert date in MySQL using PHP

Conclusion:

From above various example you come to understand regarding str_replace PHP.

I hope you like this article and if you have any query, please comment below.