宝哥软件园

用PHP用星号替换部分内容

编辑:宝哥软件园 来源:互联网 时间:2021-09-08

在最近的项目中,我们会满足某人的手机号码隐藏中间数字,身份证号码只显示最后四位数字的需求。当时我一开始在网上搜索,发现有人用函数substr_replace来代替。后来我也用了这个功能,但是不是很好用。

首先,substr_replace首先看一下这个函数的语法:

复制代码如下: substr _ replace(字符串、替换、开始、长度)

参数描述字符串是必需的。指定要检查的字符串。需要更换。指定要插入的字符串。需要启动。指定字符串中替换的开始位置。

正数-在起始偏移量处替换

负数-在从字符串末尾开始的偏移量处替换

0-替换从字符串中的第一个字符开始

Charlist是可选的。指定要替换的字符数。

正数-要替换的字符串的长度

负数-从字符串末尾替换的字符数

0-插入而不是替换

1.当start和charlist都是正数时,很容易理解和象征人类逻辑。start从0开始,如下图所示。根据条件,绿色将是要替换的元素。

2.当start是负的,charlist是正的,这是完全可以理解的

3.当开始是积极的,而查理是消极的,我一开始误解了这一点

4.当start为负,charlist为负时,有一点需要注意:如果start为负,长度小于或等于start,那么长度为0。这个坑很容易踩

5.当charlist为0时,它将被插入,而不是被替换。

用起来感觉不太舒服。虽然满足我现在的需求是可以的,但是如果以后需要一些扩展的话就很难玩了,所以我就想到自己构建一个,以后用起来会很方便。

二、自制星号替换功能

复制代码如下: replacestar ($ str,$ start,$ length=0)

前两个参数与上面相同,最后一个参数与上面不同

1.当开始和长度都是正数时,它与substr_replace相同

2.当start为负,length为正时,它与substr_replace相同

第三,源代码共享

公共静态函数replaceStar($str,$start,$ length=0){ $ I=0;$ star=if($ start=0){ if($ length 0){ $ str _ len=strlen($ str);$ count=$ length如果($start=$str_len) {//当初始下标大于字符串长度时,不会被$count=0替换;} } else if($ length 0){ $ str _ len=strlen($ str);$ count=ABS($ length);如果($start=$str_len) {//当初始下标大于字符串长度时,它从最后一个字符的下标开始,因为它是反向的。$ start=$ str _ len-1;} $ offset=$ start-$ count 1;//开始下标减去数量,计算偏移量$count=$offset=0?ABS($长度):($起始1);//如果偏移量大于等于0,说明没有超过最左侧;如果它小于0,则意味着它已经超过了最左侧,所以使用从起点到最左侧的长度$start=$offset=0?$ offset : 0;//从最左边的位置或左边的位置开始} else { $ str _ len=strlen($ str);$ count=$ str _ len-$ start;//计算要替换的数量}}否则{ if($ length 0){ $ offset=ABS($ start);$计数=$偏移量=$长度?$长度: $偏移量;//当长度大于等于时,不超过最右} else if($ length 0){ $ str _ len=strlen($ str);$ end=$ str _ len $ start//计算offset $ offset=ABS($ start $ length)-1的结束值;//计算偏移量,加起来$start=$str_len-$offset,因为都是负数;//计算起点值$start=$start=0?$ start : 0;$ count=$ end-$ start 1;} else { $ str _ len=strlen($ str);$ count=$ str _ len $ start 1;//计算要偏移的长度$ start=0;} } while ($i $count) { $star。='*';$ I;}返回substr_replace($str,$star,$start,$ count);}我不擅长算法,这里就用普通的逻辑展示一下,不使用任何数学公式。

1.如果此处为($start=0 ),则使start大于或等于0且小于0的分支。

2.在起始点中,分别制作三个长度大于0、小于0和等于0的分支

3.最后,计算开始、计数和要替换的星号字符串。计算的开始和计数都是正数,请替换substr_replace

第四,单元测试

公共函数testReplaceStar(){ $ actual=App _ Util _ string 33603360 replacestar(' 123456789 ',3,2);$ this-assertekals($实际,' 123 * * 6789 ');$ actual=App _ Util _ string :3360 replace star(' 123456789 ',9);$ this-assertekals($实际值,' 123456789 ');$ actual=App _ Util _ string :3360 replace star(' 123456789 ',9,2);$ this-assertekals($实际值,' 123456789 ');$ actual=App _ Util _ string :3360 replace star(' 123456789 ',9,-9);$ this-assertequils($实际,' * * * * * * * * * ');$ actual=App _ Util _ string :3360 replace star(' 123456789 ',9,-10);$ this-assertequils($实际,' * * * * * * * * * ');$ actual=App _ Util _ string :3360 replace star(' 123456789 ',9,-11);$ this-assertequils($实际,' * * * * * * * * * ');$ actual=App _ Util _ string :3360 replace star(' 123456789 ',3);$ this-assertekals($实际值,' 123 * * * * * * ');$ actual=App _ Util _ string :3360 replace star(' 123456789 ',0);$ this-assertequils($实际,' * * * * * * * * * ');$ actual=App _ Util _ string :3360 replace star(' 123456789 ',0,2);$ this-assertekals($实际,' * * 3456789 ');$ actual=App _ Util _ string :3360 replace star(' 123456789 ',3,-3);$ this-assertekals($实际值,'1 * * * 56789');$ actual=App _ Util _ string :3360 replace star(' 123456789 ',1,-5);$ this-assertekals($实际,' * * 3456789 ');$ actual=App _ Util _ string :3360 replace star(' 123456789 ',3,-3);$ this-assertekals($实际值,'1 * * * 56789');$ actual=App _ Util _ string :3360 replace star(' 123456789 ',-3,2);$ this-assertekals($实际值,' 123456 * * 9 ');$ actual=App _ Util _ string :3360 replace star(' 123456789 ',-3,5);$ this-assertekals($实际值,' 123456 * * * ');$ actual=App _ Util _ string :3360 replace star(' 123456789 ',-1,2);$ this-assertekals($实际值,' 12345678 * ');$ actual=App _ Util _ string :3360 replace star(' 123456789 ',-1,-2);$ this-assertekals($实际值,' 1234567 * * ');$ actual=App _ Util _ string :3360 replace star(' 123456789 ',-4,-7);$ this-assertekals($实际,' * * * * * 789 ');$ actual=App _ Util _ string :3360 replace star(' 123456789 ',-1,-3);$ this-assertekals($实际值,' 123456 * * * ');$ actual=App _ Util _ string :3360 replace star(' 123456789 ',-1);$ this-assertequils($实际,' * * * * * * * * * ');$ actual=App _ Util _ string :3360 replace star(' 123456789 ',-2);$ this-assertekals($实际值,' * * * * * * * * * 9 ');$ actual=App _ Util _ string :3360 replace star(' 123456789 ',-9);$ this-assertekals($实际,' * 23456789 ');$ actual=App _ Util _ string :3360 replace star(' 123456789 ',-10);$ this-assertekals($实际值,' 123456789 ');$ actual=App _ Util _ string :3360 replace star(' 123456789 ',-10,-2);$ this-assertekals($实际值,' 123456789 ');}以上就是本文的全部内容,希望对大家的学习有所帮助。

更多资讯
游戏推荐
更多+