百科狗-知识改变命运!
--

substr_replace() - 替换字符串的子串 - php 字符串函数

梵高1年前 (2023-11-21)阅读数 14#技术干货
文章标签字符串

substr_replace()

(PHP 4, PHP 5, PHP 7)

替换字符串的子串

说明

substr_replace(mixed $string, mixed $replacement, mixed $start[,mixed $length] ): mixed

substr_replace()在字符串$string的副本中将由$start和可选的$length参数限定的子字符串使用$replacement进行替换。

参数

$string

输入字符串。

Anarrayofstrings can be provided, in which case the replacements will occur on each string in turn. In this case,the$replacement,$startand$lengthparameters may be provided either as scalar values to be applied to each input string in turn, or asarrays, in which case the corresponding array element will be used for each input string.

$replacement

替换字符串。

$start

如果$start为正数,替换将从$string的$start位置开始。

如果$start为负数,替换将从$string的倒数第$start个位置开始。

$length

如果设定了这个参数并且为正数,表示$string中被替换的子字符串的长度。如果设定为负数,它表示待替换的子字符串结尾处距离$string末端的字符个数。如果没有提供此参数,那么它默认为 strlen($string) (字符串的长度)。当然,如果$length为 0,那么这个函数的功能为将$replacement插入到$string的$start位置处。

返回值

返回结果字符串。如果$string是个数组,那么也将返回一个数组。

更新日志

版本说明
4.3.3All parameters now acceptarrays.

范例

Example #1substr_replace()范例

Usingsubstr_replace()to replace multiple strings at once

substr_replace() - 替换字符串的子串 - php 字符串函数

以上例程会输出:

A: YYY; B: YYY; C: YYY
A: AAA; B: BBB; C: CCC
A: AAAXX; B: BBBX; C: CCC

注释

Note:此函数可安全用于二进制对象。

参见

  • str_replace()子字符串替换
  • substr()返回字符串的子串
  • 字符串访问与修改
See array_splice if you want to do this sort of thing to an array.
It's worth noting that when start and length are both negative -and- the length is less than or equal to start, the length will have the effect of being set as 0.

Same as: 


Same as: 

Another note, if length is negative and start offsets the same position as length, length (yet again) will have the effect as being set as 0. (Of course, as mentioned in the manual, when length is negative it actually represents the position before it)

Same as: 


Same as:
I've just taken a look at the post by ntoniazzi and I have a very small correction to make.
In the second if statement, it should be a triple equals, so:

It requires the triple equals, for the case of pure insertion, where $length = 0, the double equals, will catch this, causing the string to be cut short. I hope this helps someone.
Forget all of the mb_substr_replace() implementations mentioned in this page, they're all buggy.
Here is a version that mimics the behavior of substr_replace() exactly:
I recently ran across a situation where I need to strip a heavily nested html list such that only the top level was preserved. I started with a regular expression solution, but found that I kept matching the wrong closing ul with an outer opening ul.
This was my alternative solution, and it seems to work well:

Hope this helps someone.
I wrote a function that you can use for example in combination with a search script to cut off the articles that are too long.
This will truncate a longer string to a smaller string of specified length while replacing the middle portion with a separator exactly in the middle.
I have a little function that works like substr_replace () what I use for some purpose. Maybe someone needs it.

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)