str_replace()

cslylla's avatar
Published Jun 3, 2022Updated Jul 28, 2023
Contribute to Docs

The 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.

Syntax

str_replace($search, $replace, $base, $count)
  • $search is the string being searched for in $base. It can be a string or an array. Each element will be searched for if it’s an array.
  • $replace is the string used for replacing the $search term. If $search and $replace are arrays, each $search item will be replaced by the corresponding item in $replace. If $replace items run out, an empty string will be used for the remaining values. If $search is an array and $replace is a string, then the $replace value will be used for all $search items.
  • $base is 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.
  • $count is an optional int. If included, it will be set to the number of replacements performed.

Example

The following example uses the str_replace() function to replace the word "book" with "lot" in the given sentence. Then the echo command prints this sentence to the console:

<?php
echo str_replace("book", "lot", "I read a book.");
?>

The example will result in the following output:

I read a lot.

Codebyte Example

This example uses the str_replace() function with strings.

Code
Output
Loading...

All contributors

Contribute to Docs

Learn PHP on Codecademy