在指定位置插入字符串

2022-08-30 06:04:20

有没有PHP函数可以做到这一点?

我使用来获取子字符串的位置,我想在该位置之后插入一个。strposstring


答案 1
$newstr = substr_replace($oldstr, $str_to_insert, $pos, 0);

http://php.net/substr_replace

在上面的代码片段中,用于函数的参数。$posoffset

偏移 量
如果偏移量为非负数,则替换将从偏移量到字符串的第一个偏移量开始。

如果偏移量为负数,则替换将从字符串末尾的第 0 个偏移量字符开始。


答案 2
$str = substr($oldstr, 0, $pos) . $str_to_insert . substr($oldstr, $pos);

PHP 手册上的子字符串


推荐