php如何从指定位置替换字符串

  • 来源:网络
  • 更新日期:2020-09-02

摘要:在php中可以使用“substr_replace”函数实现从指定位置替换字符串,其语法是“substr_replace(string,replacement,start,length)”,参数start表示从指定位置开始替换。推荐:《P

在php中可以使用“substr_replace”函数实现从指定位置替换字符串,其语法是“substr_replace(string,replacement,start,length)”,参数start表示从指定位置开始替换。

推荐:《PHP视频教程》

php从指定位置开始替换字符方法

定义和用法

substr_replace() 函数把字符串的一部分替换为另一个字符串。

语法

substr_replace(string,replacement,start,length)
echo substr_replace("hello world","earth",6);

参数

string 必需。规定要检查的字符串。

replacement 必需。规定要插入的字符串。

start

必需。规定在字符串的何处开始替换。

正数 - 在第 start 个偏移量开始替换

负数 - 在从字符串结尾的第 start 个偏移量开始替换

0 - 在字符串中的第一个字符处开始替换

charlist

可选。规定要替换多少个字符。

正数 - 被替换的字符串长度

负数 - 从字符串末端开始的被替换字符数

0 - 插入而非替换