Home  >  Article  >  Backend Development  >  How to implement partial replacement in PHP

How to implement partial replacement in PHP

藏色散人
藏色散人Original
2021-05-08 09:23:321807browse

How to implement partial replacement in PHP: 1. Implement partial replacement through the "substr_replace" function; 2. Implement partial replacement through the self-made asterisk replacement function "replaceStar($str, $start, $length=0)" replace.

How to implement partial replacement in PHP

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

PHP realizes partial replacement, uses PHP to Replace part of the content with asterisks

In a recent project, I encountered the need to hide the middle digits of someone’s mobile phone number and only display the last 4 digits of their ID number. At the beginning, I searched online and saw that someone used the substr_replace function to replace it. I also used this function later, but it was not very useful when I used it.

1. substr_replace

Let’s first look at the syntax of this function:

The code is as follows:

substr_replace(string,replacement,start,length)
Parameters Description
##string Required. Specifies the string to check.
replacement Required. Specifies the string to be inserted.
start

Required. Specifies where in the string to begin replacement.

Positive numbers - Start replacing at the start offset

Negative numbers- Replace #start th offset from the end of the string

0 - within the string Start replacing

charlist

at the first character. Optional. Specifies how many characters to replace.

Positive number - the length of the string to be replaced

Negative number - the number of characters to be replaced from the end of the string

 0 - Insert instead of replacing

  • 当start与charlist都为正数的时候,非常好理解,也很符号人的逻辑,start是从0开始的,如下图,根据条件,绿色的将是要被替换的元素

  • 当start为负数,charlist为正数的时候,也挺好理解的

  • 当start为正数,charlist为负数的时候,这个我一开始理解错了

  • 当start为负数,charlist为负数的时候,有一个地方需要注意的就是:如果 start 是负数且 length 小于等于 start,则 length 为 0。这个坑挺容易踩到的

  • charlist为0的时候,就变成插入了,而不是替换,额。。。

用下来,我是感觉不是很顺手,虽然说满足我现在的需求还是可以的,但是如果将来需要一些扩展的话,耍起来挺吃力的,所以就想到自己构造一个,将来用起来也方便。

推荐学习:《PHP视频教程

二、自制的星号替换函数

代码如下:

replaceStar($str, $start, $length = 0)

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

  • 当start与length都为正数,与substr_replace表现的一样

  • 当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 .= &#39;*&#39;;
  $i++;
 }

 return substr_replace($str, $star, $start, $count);
}

不擅长算法,这里就用很普通的逻辑来展示啦,没有用到啥数学公式。

  • if($start >= 0)这里做start大于等于0与小于0的分支

  • 在start 的分之中,分别再做length 大于0,小于0和等于0的三个分支

  • 最后计算出start、count和要替换的星号字符串,最后计算出的start与count都是正数,运用substr_replace做替换

 单元测试


public function testReplaceStar()
 {
 $actual = App_Util_String::replaceStar(&#39;123456789&#39;, 3, 2);
 $this->assertEquals($actual, &#39;123**6789&#39;);
 
 $actual = App_Util_String::replaceStar(&#39;123456789&#39;, 9);
 $this->assertEquals($actual, &#39;123456789&#39;);
 
 $actual = App_Util_String::replaceStar(&#39;123456789&#39;, 9, 2);
 $this->assertEquals($actual, &#39;123456789&#39;);
 
 $actual = App_Util_String::replaceStar(&#39;123456789&#39;, 9, -9);
 $this->assertEquals($actual, &#39;*********&#39;);
 
 $actual = App_Util_String::replaceStar(&#39;123456789&#39;, 9, -10);
 $this->assertEquals($actual, &#39;*********&#39;);
 
 $actual = App_Util_String::replaceStar(&#39;123456789&#39;, 9, -11);
 $this->assertEquals($actual, &#39;*********&#39;);
 
 $actual = App_Util_String::replaceStar(&#39;123456789&#39;, 3);
 $this->assertEquals($actual, &#39;123******&#39;);
 
 $actual = App_Util_String::replaceStar(&#39;123456789&#39;, 0);
 $this->assertEquals($actual, &#39;*********&#39;);
 
 $actual = App_Util_String::replaceStar(&#39;123456789&#39;, 0, 2);
 $this->assertEquals($actual, &#39;**3456789&#39;);

 $actual = App_Util_String::replaceStar(&#39;123456789&#39;, 3, -3);
 $this->assertEquals($actual, &#39;1***56789&#39;);
 
 $actual = App_Util_String::replaceStar(&#39;123456789&#39;, 1, -5);
 $this->assertEquals($actual, &#39;**3456789&#39;);
 
 $actual = App_Util_String::replaceStar(&#39;123456789&#39;, 3, -3);
 $this->assertEquals($actual, &#39;1***56789&#39;);
 
 $actual = App_Util_String::replaceStar(&#39;123456789&#39;, -3, 2);
 $this->assertEquals($actual, &#39;123456**9&#39;);
 
 $actual = App_Util_String::replaceStar(&#39;123456789&#39;, -3, 5);
 $this->assertEquals($actual, &#39;123456***&#39;);
 
 $actual = App_Util_String::replaceStar(&#39;123456789&#39;, -1, 2);
 $this->assertEquals($actual, &#39;12345678*&#39;);
 
 $actual = App_Util_String::replaceStar(&#39;123456789&#39;, -1, -2);
 $this->assertEquals($actual, &#39;1234567**&#39;);
 
 $actual = App_Util_String::replaceStar(&#39;123456789&#39;, -4, -7);
 $this->assertEquals($actual, &#39;******789&#39;);
 
 $actual = App_Util_String::replaceStar(&#39;123456789&#39;, -1, -3);
 $this->assertEquals($actual, &#39;123456***&#39;);
 
 $actual = App_Util_String::replaceStar(&#39;123456789&#39;, -1);
 $this->assertEquals($actual, &#39;*********&#39;);
 
 $actual = App_Util_String::replaceStar(&#39;123456789&#39;, -2);
 $this->assertEquals($actual, &#39;********9&#39;);
 
 $actual = App_Util_String::replaceStar(&#39;123456789&#39;, -9);
 $this->assertEquals($actual, &#39;*23456789&#39;);
 
 $actual = App_Util_String::replaceStar(&#39;123456789&#39;, -10);
 $this->assertEquals($actual, &#39;123456789&#39;);
 
 $actual = App_Util_String::replaceStar(&#39;123456789&#39;, -10, -2);
 $this->assertEquals($actual, &#39;123456789&#39;);
 }

The above is the detailed content of How to implement partial replacement in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn