时间:2021-07-01 10:21:17 帮助过:43人阅读
一、substr_replace
先来看看这个函数的语法:
代码如下:
substr_replace(string,replacement,start,length)
代码如下: replaceStar($str, $start, $length = 0) 前面的两个参数与上面的一样,最后的参数与上面不同 1、当start与length都为正数,与substr_replace表现的一样 2、当start为负数,length为正数,与substr_replace表现的一样 三、源码分享
public static function replaceStar($str, $start, $length = 0) { $i = 0; $star = ''; if($start >= 0) { if($length > 0) { $str_len = strlen($str); $count = $length; if($start >= $str_len) {//当开始的下标大于字符串长度的时候,就不做替换了 $count = 0; } }elseif($length < 0){ $str_len = strlen($str); $count = abs($length); if($start >= $str_len) {//当开始的下标大于字符串长度的时候,由于是反向的,就从最后那个字符的下标开始 $start = $str_len - 1; } $offset = $start - $count + 1;//起点下标减去数量,计算偏移量 $count = $offset >= 0 ? abs($length) : ($start + 1);//偏移量大于等于0说明没有超过最左边,小于0了说明超过了最左边,就用起点到最左边的长度 $start = $offset >= 0 ? $offset : 0;//从最左边或左边的某个位置开始 }else { $str_len = strlen($str); $count = $str_len - $start;//计算要替换的数量 } }else { if($length > 0) { $offset = abs($start); $count = $offset >= $length ? $length : $offset;//大于等于长度的时候 没有超出最右边 }elseif($length < 0){ $str_len = strlen($str); $end = $str_len + $start;//计算偏移的结尾值 $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++; } return substr_replace($str, $star, $start, $count); } 不擅长算法,这里就用很普通的逻辑来展示啦,没有用到啥数学公式。 1、if($start >= 0)这里做start大于等于0与小于0的分支 2、在start 的分之中,分别再做length 大于0,小于0和等于0的三个分支 3、最后计算出start、count和要替换的星号字符串,最后计算出的start与count都是正数,运用substr_replace做替换 四、单元测试
public function testReplaceStar() { $actual = App_Util_String::replaceStar('123456789', 3, 2); $this->assertEquals($actual, '123**6789'); $actual = App_Util_String::replaceStar('123456789', 9); $this->assertEquals($actual, '123456789'); $actual = App_Util_String::replaceStar('123456789', 9, 2); $this->assertEquals($actual, '123456789'); $actual = App_Util_String::replaceStar('123456789', 9, -9); $this->assertEquals($actual, '*********'); $actual = App_Util_String::replaceStar('123456789', 9, -10); $this->assertEquals($actual, '*********'); $actual = App_Util_String::replaceStar('123456789', 9, -11); $this->assertEquals($actual, '*********'); $actual = App_Util_String::replaceStar('123456789', 3); $this->assertEquals($actual, '123******'); $actual = App_Util_String::replaceStar('123456789', 0); $this->assertEquals($actual, '*********'); $actual = App_Util_String::replaceStar('123456789', 0, 2); $this->assertEquals($actual, '**3456789'); $actual = App_Util_String::replaceStar('123456789', 3, -3); $this->assertEquals($actual, '1***56789'); $actual = App_Util_String::replaceStar('123456789', 1, -5); $this->assertEquals($actual, '**3456789'); $actual = App_Util_String::replaceStar('123456789', 3, -3); $this->assertEquals($actual, '1***56789'); $actual = App_Util_String::replaceStar('123456789', -3, 2); $this->assertEquals($actual, '123456**9'); $actual = App_Util_String::replaceStar('123456789', -3, 5); $this->assertEquals($actual, '123456***'); $actual = App_Util_String::replaceStar('123456789', -1, 2); $this->assertEquals($actual, '12345678*'); $actual = App_Util_String::replaceStar('123456789', -1, -2); $this->assertEquals($actual, '1234567**'); $actual = App_Util_String::replaceStar('123456789', -4, -7); $this->assertEquals($actual, '******789'); $actual = App_Util_String::replaceStar('123456789', -1, -3); $this->assertEquals($actual, '123456***'); $actual = App_Util_String::replaceStar('123456789', -1); $this->assertEquals($actual, '*********'); $actual = App_Util_String::replaceStar('123456789', -2); $this->assertEquals($actual, '********9'); $actual = App_Util_String::replaceStar('123456789', -9); $this->assertEquals($actual, '*23456789'); $actual = App_Util_String::replaceStar('123456789', -10); $this->assertEquals($actual, '123456789'); $actual = App_Util_String::replaceStar('123456789', -10, -2); $this->assertEquals($actual, '123456789'); } 以上就是本文的全部内容,希望对大家的学习有所帮助。 人气教程排行
|