PHP的substr_replace将指定两位置之间的字符替换为*号

  作者:bea

代码如下: $username = "zongzi"; echo substr_replace($username,'**','1','2'); 定义和用法 substr_replace() 函数把字符串的一部分替换为另一个字符串。 语法 substr_replace(string,replacement,start,length) 参数 描述 string 必需。规定要检查的字符串。

代码如下:


$username = "zongzi";
echo substr_replace($username,'**','1','2');




定义和用法
substr_replace() 函数把字符串的一部分替换为另一个字符串。
语法
substr_replace(string,replacement,start,length)



参数
描述


string
必需。规定要检查的字符串。


replacement
必需。规定要插入的字符串。


start
必需。规定在字符串的何处开始替换。

正数 - 在第 start 个偏移量开始替换
负数 - 在从字符串结尾的第 start 个偏移量开始替换
0 - 在字符串中的第一个字符处开始替换



charlist
可选。规定要替换多少个字符。

正数 - 被替换的字符串长度
负数 - 从字符串末端开始的被替换字符数
0 - 插入而非替换






提示和注释
注释:如果 start 是负数且 length 小于等于 start,则 length 为 0。例子

代码如下:


<?php
echo substr_replace("Hello world","earth",6);
?>


输出:
Hello earth




有用  |  无用

猜你喜欢