str_replace() function returns a string with occurrences of a specified substring replaced by another string. The function can operate on scalar strings or arrays.
str_replace($search, $replace, $base, $count)
$searchis the string being searched for in
$base. It can be a string or an array. If it’s an array, each element will be searched for in turn.
$replaceis the string used for replacing the
$replaceare arrays, each
$searchitem will be replaced by the corresponding item in
$replaceitems run out, then an empty string will be used for remaining values. If
$searchis an array and
$replaceis a string, then the
$replacevalue will be used for all
$baseis the string(s) being searched. If it is an array, the replacements will be made to every string in the array, and the function will return an array of the modified strings.
$countis an optional
int. If included, it will be set to the number of replacements performed.
This example provides three demonstrations of
str_replace() operating on a string.
Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.