In PHP, you can use the substr_replace() function to replace the first character of a string; this function can replace a specified number of characters starting from the specified position in the string. You only need to set the third parameter is "0", the fourth parameter is set to 1 to replace the first character, the syntax is "substr_replace(string, replacement character, 0, 1)".
The operating environment of this tutorial: windows7 system, PHP8.1 version, DELL G3 computer
In php, You can use the substr_replace() function to replace the first character of a string.
The substr_replace() function can replace the specified number of characters starting from the specified position in the string.
If you want to replace the first character of the string, you only need to set the third parameter to "0" and the fourth parameter to 1. (The index of characters in the string starts with 0, so the starting position of the first character is 0)
Example:
<?php header('content-type:text/html;charset=utf-8'); $str = 'hello world!'; echo "原字符串:".$str."<br><br>"; $replace = 'A'; echo "替换第一位字符:".substr_replace($str, $replace,0,1)."<br>"; ?>
Description: substr_replace() function
The 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.
|
4 ways to use str_replace() function
1. String replacement string
<?php //实例一:字符串替换字符串 $str1 = str_replace("red","black","red green yellow pink purple"); echo $str1.""; //输出结果为black green yellow pink purple ?>
Output:
2. String replacement array key value
<?php //实例二:字符串替换数组键值 $arr = array("blue","red","green","yellow"); $str1 = str_replace("red","pink",$arr,$i); var_dump($str1); ?>
Output:
3 , Array replaces array, mapping replaces
<?php //实例三:数组替换数组,映射替换 $arr1 = array("banana","orange"); $arr2 = array("pitaya","tomato"); $con_arr = array("apple","orange","banana","grape"); $con_rep = str_replace($arr1,$arr2,$con_arr,$count); var_dump($con_rep); ?>
Output:
##4. If $search is an array and $replace is a string<?php //实例四:如$search为数组,$replace为字符串时 $search = array("banana","grape"); $replace = "tomato"; $arr = array("banana","apple","orange","grape"); $new_arr = str_replace($search,$replace,$arr,$count); var_dump($new_arr); ?>
PHP Video Tutorial"
The above is the detailed content of How to replace the first character of a string in php. For more information, please follow other related articles on the PHP Chinese website!