substr_replace() - 替换字符串的子串 - php 字符串函数
substr_replace()
(PHP 4, PHP 5, PHP 7)
替换字符串的子串
说明
substr_replace(mixed $string, mixed $replacement, mixed $start[,mixed $length] ): mixedsubstr_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.3 | All parameters now acceptarrays. |
范例
Example #1substr_replace()范例
Usingsubstr_replace()to replace multiple strings at once
以上例程会输出:
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
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!