PHP str_replace Function is an inbuilt function in PHP. It replaces all the occurrences of a string or an array of string in another string or an array of string respectively. In this article, we will discuss the PHP str_replace Function. Also, we will discuss a few examples of using it.
str_replace ( $search, $replace, $subject, $count )
The PHP str_replace Function expects four parameters. However, only three parameters are mandatory and one is optional. The description of the parameters is as follows:
- $search: The search parameter specifies the string to search. Also, it can be an array to specify multiple search strings.
- $replace: The replace parameter specifies the string to replace with. Also, it can be an array to specify multiple replacements.
- $subject: The subject string is the string or an array of string which will be searched and replaced on.
- $count: The count parameter, if passed, is set to the total number of replacements done by the function. It is an optional parameter.
Points to Remember
Always remember the following points while working with str_replace function in PHP.
- If search and replace are arrays, then the function takes a value from each array and uses them to search and replace on the subject.
- Also, If replace has lesser values than search string, then the function assumes blank strings for the remaining values.
- If the search is an array and replace is a string, then the function searches for all values from search array and replaces the single string. However, the opposite is not true.
- If the subject is an array, then the function performs search and replace on every element of the array. Also, the return type of the function is also an array in this case.
The PHP str_replace Function returns a string or an array of strings after replacing values.
Let’s discuss a few examples of using the function.
Example 1: Simple String Replacement
For example, consider a simple string to replace.
<?php $search = 'World'; $replace = 'There'; $subject = 'Hello World!'; $finalString = str_replace($search, $replace, $subject); echo $finalString; ?>
In the above example, the str_replace function in PHP replaces all occurrences of ‘World’ with ‘There’ in the subject string.
OUTPUT: Hello There!
Example 2: Array Search and Single Replacement
Now, consider an example with search parameter as array of strings and replacement as a single string.
<?php $search = array( 'a', 'e' ,'i', 'o', 'u' ); $replace = 'XYZ'; $subject = 'Welcome to Concatly!'; $finalString = str_replace($search, $replace, $subject); echo $finalString; ?>
In the above example, the function searches for all values from search array and replaces the single replacement string for all of them.
OUTPUT: WXYZlcXYZmXYZ tXYZ CXYZncXYZtly!
Example 3: Both Search and Replace are Arrays
For instance, consider an example with both search and replace strings as arrays.
<?php $search = array( 'Dog', 'Cat', 'Mouse' ); $replace = array( 'Lion', 'Zebra', 'Goat' ); $subject = 'There was a Dog, a Cat and a Mouse in the Zoo'; $finalString = str_replace($search, $replace, $subject); echo $finalString; ?>
The PHP str_replace function takes a value from each array and uses them to search and replace on the subject. The replacement occurs in the following way:
Dog -> Lion
Cat -> Zebra
Mouse -> Goat
OUTPUT: There was a Lion, a Zebra and a Goat in the Zoo
Example 4: Array of Subject
Similarly, we can pass an array in the subject parameter. In this case, the function performs search and replace on every element of the array.
<?php $search = 'world'; $replace = 'randomstring'; $subject = array( 'Hello world', 'the world is big', 'save the world' ); $finalArray = str_replace($search, $replace, $subject); print_r($finalArray); ?>
OUTPUT: Array (  => Hello randomstring  => the randomstring is big  => save the randomstring )
Example 5: Passing the Count Parameter
Also, we can pass the count parameter to the PHP str_replace function. The variable is set to the total number of replacements done by the function.
<?php $str = str_replace("ll", "", "good golly miss molly!", $count); echo $count; ?>
The string is replaced twice in the above example. Hence, the count value is 2.
Vishesh is currently working as a Senior Software Engineer at Naukri.com. He passed out of Delhi College of Engineering in 2016 and likes to play Foosball. He loves traveling and is an exercise freak. His expertise includes Java, PHP, Python, Databases, Design and Architecture.