Home > Article > Backend Development > How to replace characters in string in php
The way PHP replaces characters in a string is to use the str_replace() function. This function returns a string with the replacement value, or an array if the searched string is an array.
#To replace characters in a string, you can use the str_replace() function.
(Recommended tutorial: php tutorial)
Function introduction:
str_replace() function is used to replace some characters in a string (size-sensitive Write). This function returns a string or array with replacement values.
Replacement 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.
Function syntax:
str_replace(find,replace,string,count)
Parameter description:
find Required. Specifies the value to look for.
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.
Code:
The above is the detailed content of How to replace characters in string in php. For more information, please follow other related articles on the PHP Chinese website!