Home > Article > Backend Development > Use PHP to replace part of the content with asterisks and replace the php content with asterisks_PHP tutorial
In a recent project, I encountered someone’s mobile phone number hiding the middle digits. Only the last 4 digits of the ID number are required. 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 take a look at the syntax of this function first:
Copy code 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 number - start replacing at offset start Negative numbers - start replacing at start offset from the end of the string0 - Start replacing at the first character in the string |
charlist |
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 replace |
1. When start and charlist are both positive numbers, it is very easy to understand and symbolizes human logic. Start starts from 0, as shown below. According to the conditions, the green element will be the element to be replaced
2. When start is a negative number and charlist is a positive number, it is easy to understand
3. When start is a positive number and charlist is a negative number, I misunderstood this at first
4. When start is a negative number and charlist is a negative number, one thing to note is: if start is a negative number and length is less than or equal to start, then length is 0. This trap is quite easy to step into
5. When charlist is 0, it becomes insertion instead of replacement, eh. . .
After using it, I feel that it is not very smooth. Although it can meet my current needs, if I need some expansion in the future, it will be quite difficult to play, so I thought of constructing one myself, which will be convenient to use in the future. .
2. Homemade asterisk replacement function
Copy code The code is as follows: replaceStar($str, $start, $length = 0)
The first two parameters are the same as above, and the last parameter is different from above
1. When start and length are both positive numbers, it behaves the same as substr_replace
2. When start is a negative number and length is a positive number, it behaves the same as substr_replace
3. Source code sharing
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); }
I am not good at algorithms, so I will use very common logic to show it here, without using any mathematical formulas.
1. if($start >= 0) here is the branch where start is greater than or equal to 0 and less than 0
2. Among the start points, make three branches with length greater than 0, less than 0 and equal to 0 respectively
3. Finally calculate start, count and the asterisk string to be replaced. The finally calculated start and count are both positive numbers. Use substr_replace to replace them
4. Unit Test
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'); }
The above is the entire content of this article, I hope it will be helpful to everyone’s study.