实例
把 "Hello" 替换成 "world":
<?php
echo substr_replace("Hello","world",0);
?>
定义和用法
substr_replace() 函数把字符串的一部分替换为另一个字符串。
注释:如果 start 参数是负数且 length 小于或者等于 start,则 length 为 0。
注释:该函数是二进制安全的。
语法
substr_replace(string,replacement,start,length)
参数 | 描述 |
string | 必需。规定要检查的字符串。 |
replacement | 必需。规定要插入的字符串。 |
start | 必需。规定在字符串的何处开始替换。
|
length | 可选。规定要替换多少个字符。默认是与字符串长度相同。
|
技术细节
返回值: | 返回被替换的字符串。如果 string 是一个数组,则返回数组。 |
PHP 版本: | 4+ |
更新日志: | 自 PHP 4.3.3 起,所有参数都接受数组。 |
更多实例
实例 1
从字符串的第 6 个位置开始替换(把 "world" 替换成 "earth"):
<?php
echo substr_replace("Hello world","earth",6);
?>