Easy Tutorial
❮ Func Date Modify Php Is_Bool Function ❯

PHP str_replace() Function

PHP String Reference Manual

Example

Replace the string "world" in "Hello world!" with "Peter":


Definition and Usage

The str_replace() function replaces some characters with some other characters in a string (case-sensitive).

The function must follow these rules:

Note: This function is case-sensitive. Use the str_ireplace() function for case-insensitive search.

Note: This function is binary-safe.


Syntax

Parameter Description
find Required. Specifies the value to find.
replace Required. Specifies the value to replace the value in find.
string Required. Specifies the string to be searched.
count Optional. A variable that counts the number of replacements.

Technical Details

Return Value: Returns a string or an array with replaced values.
PHP Version: 4+
--- ---
Changelog: Added the count parameter in PHP 5.0. <br> <br>Before PHP 4.3.3, when both find and replace are arrays, this function would encounter problems. Empty find indexes would be ignored before the internal pointer was updated to the replace array. Newer versions do not have this issue. <br> <br>Since PHP 4.0.5, most parameters can be arrays.
--- ---

More Examples

Example 1

Using the str_replace() function with arrays and the count variable:

Example 2

Using the str_replace() function with fewer elements to replace than found:


❮ Func Date Modify Php Is_Bool Function ❯