3 replacement methods: 1. Use substr_replace() to replace all characters starting from the head of the string, the syntax is "substr_replace (original string, specify replacement value, 0)". 2. Use str_replace() to replace all characters, the syntax is "str_replace(original string, specified replacement value, original string)". 3. Use str_ireplace() to replace all characters, the syntax is "str_ireplace(original string, specified replacement value, original string)".
The operating environment of this tutorial: windows7 system, PHP8.1 version, DELL G3 computer
Method 1: Use substr_replace() function
substr_replace() function replaces part of a string with another string.
substr_replace(string,replacement,start,length)
substr_replace() Replaces the substring qualified by the start and optional length parameters with replacement in a copy of string string.
If start is a positive number, replacement will start from the start position of string. If start is negative, the replacement will start at the start position from the bottom of string.
If the length parameter is set and is a positive number, it represents the length of the replaced substring in string. If set to a negative number, it represents the number of characters from the end of the substring to be replaced from the end of string. If this parameter is not provided, the default is strlen(string) (the length of the string). Of course, if length is 0, then the function of this function is to insert replacement at the start position of string.
Parameters | 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.
|
length | Optional. Specifies how many characters to replace. The default is the same as the string length.
|
Example: Replace all characters in a string
Just set the third parameter of the function to 0, the third Set each parameter to the length of the original string or omit it to replace all characters
<?php header('content-type:text/html;charset=utf-8'); $str = 'hello world!'; echo "原字符串:".$str."<br><br>"; $replace = 'ABCDEFGHIJKL'; echo "替换全部字符字符:".substr_replace($str, $replace,0)."<br>"; ?>
##Method 2/Method 3: str_ireplace() and str_replace() Function
str_ireplace() and str_replace both use a new string to replace the specified substring in the original string. If the substring to be replaced is the original string, you can replace the original string. All characters in the string. The syntax of str_ireplace() and str_replace is similar, the difference is that str_replace is case-sensitive, str_ireplace() is not case-sensitivestr_replace(find,replace,string,count) str_ireplace(find,replace,string,count)
Description | |
---|---|
find | Required. Specifies the value to look for.|
replace | Required. Specifies a value that replaces the value infind. |
string | Required. Specifies the string to be searched for.|
count | Optional. Variable counting the number of substitutions.