How To Implement str_replace in PHP?

Published on Jul 29,2019 1.3K Views

How To Implement str_replace in PHP?

edureka.co

Working with character patterns has become very important these days as there is large amount data processing that happens these days. In this article we will take a look at str_replace in PHP to try and understand the concept in detail.

Following pointers will be covered in this article,

So let us get started with this article on str_replace in PHP,

str_replace in PHP

In order to replace some characters with some other characters in the string, we have functionality in PHP which works with some specific rules. They are:

Moving on with this article on str_replace in PHP, let take a look at the following function,

What is str_replace()?

str_replace() is a built-in function which is used for replacing some characters with some other characters in a string. i.e replaces all the occurrences of a word within the string.

Syntax: str_replace(find,replace,string,count);

Find: This parameter can be either string or array type which specifies the string to be searched and replaced.

Replace: This parameter can also be either string or array type which specifies the string that we want to replace with.

String: This parameter specifies the string or array of strings where we want to search and replace.

Count: This parameter is optional where the value is set to the total number of replacement operations.

Let’s see an example where the perameter for finding and replacing is a string type:

<?php
$str = 'ashok love to code because ashok wants to get placed in a good product based company';
echo str_replace("ashok", "sushma", $str);
?>
sushma love to code because sushma wants to get placed in a good product based company

In the above example, every occurrence of “ashok” is replaced with “sushma”.

Lets see another example where the parameter for finding and replacing is an array type:

<?php
$str = "Your friends are Tarun, Charan, Adarsh, Sabid";
// Array that contains the elements to be searched
$find = array("Tarun", "Charan", "Sabid");
// Array that contains the string s tor replaced with searched string
$replace= array("Sushma", "Vaibhav", "Chintan");
$output = str_replace($find, $replace, $str);
print_r($output);
?>

Output

Your friends are Sushma, Vaibhav, Adarsh, Chintan

When the parameters are of array type, all the elements of the find argument are searched in the string and replaced by the corresponding elements in the replace argument. If the number of elements in replace argument is less than that in find array, then if there are any occurrences of the additional elements of find argument in the string argument then they will be replaced by an empty string. If the string parameter is also an array instead of string then all of the elements of string will be searched.

This function is case-sensitive and in order to overcome this problem, we have str_ireplace() to perform a case-insensitive match.

Moving on with this article on str_replace in PHP, let take a look at the following function,

What is str_ireplace()?

str_ireplace() is a built-in function which is similar to str_replace(), i.e  replaces characters which are even case-sensitive characters in a string.

Syntax: str_ireplace(find,replace,string,count);

Let’s see an example where the parameter for finding and replacing is a string type(irrespective of case-sensitive string):

<?php
$str = 'ashok love to code because Ashok wants to get placed in a good product based company';
echo str_ireplace("ashok", "sushma", $str);
?>

Output:

sushma love to code because sushma wants to get placed in a good product based company

Let’s see another example where the parameter for finding and replacing is an array type (irrespective of case-sensitive string):

<?php
$str = "Your friends are Tarun, Charan, Adarsh, Sabid";
// Array that contains the elements to be searched
$find = array("tarUn", "ChaRan", "saBiD");
// Array that contains the string s tor replaced with searched string
$replace= array("Sushma", "Vaibhav", "Chintan");
$output = str_ireplace($find, $replace, $str);
print_r($output);
?>

Output

Your friends are Sushma, Vaibhav, Adarsh, Chintan

With this we come to an end of this article, I hope you have learned about both the inbuilt functions in PHP,i.e str_replace() and str_ireplace() with suitable examples.

If you found this str_replace in PHP article relevant, check out the PHP Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.

Got a question for us? Please mention it in the comments section of  this article and I will get back to you.

BROWSE COURSES
REGISTER FOR FREE WEBINAR Building Robust Applications with Spring Framework