小白教程
所有教程
关于
Search
172.69.58.172
172.69.58.172
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Func string substr replace”的源代码
本页内容
上一节:
Func_string_substr_count
下一节:
Func_string_trim
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:substr_replace()}}[[分类:PHP字符串函数参考手册|93]] = PHP substr_replace() 函数 = [[:分类:PHP字符串函数参考手册]] 把 "Hello" 替换成 "world": <sample title="" desc="" lang="php" hererun="1"> <?php echo substr_replace("Hello","world",0); ?> </sample> == 定义和用法 == substr_replace() 函数把字符串的一部分替换为另一个字符串。 '''注释:'''如果 start 参数是负数且 length 小于或者等于 start,则 length 为 0。 '''注释:'''该函数是二进制安全的。 == 语法 == <pre> substr_replace(string,replacement,start,length) </pre> {| class="table table-striped table-hover" !width="50%"| 参数 !width="50%"| 描述 |- | string | 必需。规定要检查的字符串。 |- | replacement | 必需。规定要插入的字符串。 |- | start | 必需。规定在字符串的何处开始替换。 * 正数 - 在字符串的指定位置开始 * 负数 - 在从字符串结尾的指定位置开始 * 0 - 在字符串中的第一个字符处开始 |- | length | 可选。规定要替换多少个字符。默认是与字符串长度相同。 * 正数 - 被替换的字符串长度 * 负数 - 从字符串末端开始的被替换字符数 * 0 - 插入而非替换 |} == 技术细节 == {| class="table table-striped table-hover" | 返回值: | 返回被替换的字符串。如果 string 是一个数组,则返回数组。 |- | PHP 版本: | 4+ |- | 更新日志: | 自 PHP 4.3.3 起,所有参数都接受数组。 |} == 更多实例 == == 实例 1 == 从字符串的第 6 个位置开始替换(把 "world" 替换成 "earth"): <sample title="" desc="" lang="php" hererun="1"> <?php echo substr_replace("Hello world","earth",6); ?> </sample> == 实例 2 == 从字符串结尾的第 5 个位置开始替换(把 "world" 替换成 "earth"): <sample title="" desc="" lang="php" hererun="1"> <?php echo substr_replace("Hello world","earth",-5); ?> </sample> == 实例 3 == 在 "world" 开头插入 "Hello": <sample title="" desc="" lang="php" hererun="1"> <?php echo substr_replace("world","Hello ",0,0); ?> </sample> == 实例 4 == 一次性替换多个字符串。把每个字符串中的 "AAA" 替换成 "BBB": <sample title="" desc="" lang="php" hererun="1"> <?php $replace = array("1: AAA","2: AAA","3: AAA"); echo implode("<br>",substr_replace($replace,'BBB',3,3)); ?> </sample> [[:分类:PHP字符串函数参考手册]]
返回至“
Func string substr replace
”。
上一节:
Func_string_substr_count
下一节:
Func_string_trim