Home >Backend Development >PHP Tutorial >PHP function str_replace() that replaces some characters in a string (case-sensitive)
Example
Replace the character "world" in String "Hello world!" with "Peter":
<?php echo str_replace("world","Peter","Hello world!"); ?>
Definition and usage
str_replace() function replaces some characters in a string (case sensitive).
This function must follow the following rules:
If the searched string is an array, then it will return an array.
If the searched string is an array, then it will find and replace each element in the array.
If an array needs to be searched and replaced at the same time, and the elements to be replaced are less than the number of found elements, the excess elements will be replaced with empty strings.
If you search an array and replace only one string, the replacement string will work on all found values.
Note: This function is case-sensitive. Please use the str_ireplace() function to perform a case-insensitive search.
Note: This function is binary safe.
Syntax
str_replace(find,replace,string,count)
Parameters | 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 for. |
count | Optional. A variable counting the number of substitutions. |
Technical details
Return value: | Returns a string with the replacement value or array. |
PHP Version: | 4+ |
##Update Log : | In PHP 5.0, the count parameter was added.
Before PHP 4.3.3, trouble would be encountered when the find and replace parameters of this function were both arrays, which would cause the empty find index to be ignored when the internal pointer was not replaced on the replace array. The new version won't have this problem. Since PHP 4.0.5, most parameters can be an array. |
The above is the detailed content of PHP function str_replace() that replaces some characters in a string (case-sensitive). For more information, please follow other related articles on the PHP Chinese website!