Home > Article > Backend Development > php string replacement function
There are several character replacement functions in php: str_replace, substr_replace, preg_replace, preg_split, str_split and other functions:
1, Str_replace() function:
Use one string to replace other characters in the string. (Case sensitive, binary safe)
(1) Format: str_replace(find,replace,string,count)
(2)Parameter Description:
find Required. Specifies the value to be found.
replace Required. Specifies a value that replaces the value in find .
string Required. Specifies the string to be searched for.
count Optional. A variable that counts the number of substitutions
Added: countIf specified, its value will be set to the number of substitutions that occurred
2, str_ireplace() Function:
Use a string to replace other characters in a string. (Case insensitive, binary safe)
(1) Format: str_ireplace(find,replace,string,count)
(2) Parameters are the same as above
3, substr_replace() Function:
Replace part of a string with another string. (1)
(1) Format: substr_replace(string,replacement,start,length)
(2) Parameter Description
string Required. Specifies the string to check.
replacement Required. Specifies the string to be inserted.
start Required. Specifies where in the string to begin replacement.
Positive numbers - Start replacing negative numbers at the start offset
0 - Start replacing charlist optional at the first character in the string. Specifies how many characters to replace.
Positive number- The length of the replaced string
Negative number -
The number of replaced characters from the end of the string0 - Insertion instead of replacement
Note: If start is a negative number and
length is less than or equal to
start, then length is 0. Function : Perform a regular expression search and replace (2) parameter description pattern required. The pattern to search for.
replacement Required. String or array to use for replacement. subject Required. String or array to be replaced. limit Number of replacements. -1
is unlimited count The number of times to complete the replacement, variables
5, preg_split ( pattern , subject
, limit = -1
, flag )
(1
) Function: Split the string by regular expression (2) Parameter Description
pattern Required. The pattern to search for.
lacement Required. String or array to use for replacement. subject Required. The string that needs to be replaced. limit The most split stringslimit
. flag mode
6, str_split (subject
, length)
(1
) Function: Split the string into an array
(2) Parameter Description subject String.
length The length of each paragraph.
The above has introduced the PHP string replacement function, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.