在PHP中,可以使用substr_replace()函数来替换字符串中第一个字符,语法格式“substr_replace(原字符串,"替换字符",0,1)”;substr_replace()函数可以把字符串的一部分替换为另一个字符串。

本教程操作环境:windows7系统、PHP7.1版,DELL G3电脑 php替换字符串中第一个字符 <?php
$str1="abcdefg";
$str2=substr_replace($str1,"A",0,1);
echo $str2;
?> 输出: Abcdefg 相关函数说明:
substr_replace() 函数把字符串的一部分替换为另一个字符串。 语法: substr_replace(string,replacement,start,length) 参数 | 描述 |
---|
string | 必需。规定要检查的字符串。 | replacement | 必需。规定要插入的字符串。 | start | 必需。规定在字符串的何处开始替换。 - 正数 - 在字符串中的指定位置开始替换
- 负数 - 在从字符串结尾的指定位置开始替换
- 0 - 在字符串中的第一个字符处开始替换
| length | 可选。规定要替换多少个字符。默认是与字符串长度相同。 - 正数 - 被替换的字符串长度
- 负数 - 表示待替换的子字符串结尾处距离 string 末端的字符个数。
- 0 - 插入而非替换
|
返回值: 返回被替换的字符串。如果 string 是数组,则返回数组。 推荐学习:《PHP视频教程》 以上就是php怎么替换字符串中第一个字符的详细内容,更多请关注模板之家(www.mb5.com.cn)其它相关文章! |