PHP Function Reference

PHP str_replace() Function



The PHP str_replace() function returns a string or an array with all occurrences of search in subject replaced with the given replace value.

If search and replace are arrays, then str_replace() takes a value from each array and uses them to search and replace on subject. If replace has fewer values than search, then an empty string is used for the rest of replacement values. If search is an array and replace is a string, then this replacement string is used for every value of search.

Note: This function replaces left to right, it might replace a previously inserted value when doing multiple replacements.

Note: This function is case-sensitive. For case-insensitive replace, str_ireplace() function can be used.

Syntax

str_replace(search, replace, subject, count)

Parameters

search Required. Specify value being searched for. An array may be used to designate multiple values to be searched for.
replace Required. Specify replacement value that replaces found search values. An array can be used to designate multiple replacements.
subject Required. Specify string or array being searched and replaced on. If it is an array, then the search and replace is performed with every element of the array, and return value is an array.
count Optional. If passed, this will be set to the number of replacements performed.

Return Value

Returns a string or an array with the replaced values.

Example:

In the example below, every occurrence of "Hello" in str is replaced by "Hi" using str_replace() function.

<?php
$str = "Hello World, Hello PHP, Hello Programming.";

$replaced_str = str_replace("Hello", "Hi", $str);
echo $replaced_str."\n";

//i is used to find the count of replacement
$replaced_str = str_replace("Hello", "Hi", $str, $i);
echo "Count of replacement: ".$i;   
?>

The output of the above code will be:

Hi World, Hi PHP, Hi Programming.
Count of replacement: 3

Example:

In the example below, every occurrence of "Hello" in Arr is replaced by "Hi" using str_replace() function.

<?php
$Arr = array("Hello", "World", "Hello", "PHP", "Hello", "Programming");

$replaced_Arr = str_replace("Hello", "Hi", $Arr);
print_r ($replaced_Arr);

//i is used to find the count of replacement
$replaced_Arr = str_replace("Hello", "Hi", $Arr, $i);
echo "\nCount of replacement: ".$i;  
?>

The output of the above code will be:

Array
(
    [0] => Hi
    [1] => World
    [2] => Hi
    [3] => PHP
    [4] => Hi
    [5] => Programming
)

Count of replacement: 3

Example:

Consider one more example, where the search and replace parameters are provided as arrays. Every element of search array present in the given string will be replaced by the respective element of replace array.

<?php
$str = "The quick brown fox jumps over the lazy dog.";

$search = ['fox', 'dog', 'brown'];
$replace = ['wolf', 'cat', 'black'];

$result = str_replace($search, $replace, $str);

echo $result; 
?>

The output of the above code will be:

The quick black wolf jumps over the lazy cat.

❮ PHP String Reference