Home > Backend Development > PHP Tutorial > PHP replaces part of the content with asterisks_PHP tutorial

PHP replaces part of the content with asterisks_PHP tutorial

WBOY
Release: 2016-07-13 10:19:50
Original
893 people have browsed it

PHP replaces 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 the 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 take a look at the syntax of this function:
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 the start offset
Negative numbers - replace
starting at the start offset from the end of the string
0 - 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 starting 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 use, so I thought of constructing one myself, which will be convenient to use in the future. .
2. Homemade asterisk replacement function
replaceStar($str, $start, $length = 0)
Parameters Description
str Required. Specifies the string to check.
start
Required. Specifies where in the string to begin replacement.
Positive number - start replacing
at the start offset
Negative numbers - replace
starting at the start offset from the end of the string
0 - Start replacing
at the first character in the string
length
Optional. Specifies how many characters to replace.
Positive number - the length of the string to be replaced, from left to right
Negative number - the length of the string to be replaced, from right to left
0 - If start is a positive number, start from start and go left to the end
  - If start is a negative number, start from start and go to the right to the end
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
substr_replace
replaceStar
start is a positive number and length is a negative number
start is a negative number and length is a negative number
start is a positive number and the length is 0 Perform insertion operation
start is a negative number and the length is 0 Perform insertion operation
3. Source code sharing
Copy code
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 & GT; = $ Str_len) {// When the starting bid is greater than the length of the string, it will not replace it
                $count = 0;
        }
        }elseif($length < 0){
$str_len = strlen($str);
$count = abs($length);
                                                                                                                                                                                                                                                                                                   
                  $start = $str_len - 1;
        }
                                                                                                                                                                            $offset = $start - $count + 1;//Subtract the quantity from the starting point subscript and calculate the offset
                                                                                                                                         Use the length from the starting point to the far left
                                                                                                                                                                                                                                             
      }else {
$str_len = strlen($str);
                  $count = $str_len - $start;//Calculate the quantity to be replaced
      }
}else {
if($length > 0) {
$offset = abs($start);
$count = $offset >= $length ? $length : $offset;//When greater than or equal to the length, it does not exceed the rightmost
        }elseif($length < 0){
$str_len = strlen($str);
                  $end = $str_len + $start;//Calculate the end value of the offset
$offset = abs($start + $length) - 1;//Calculate the offset, since they are all negative numbers, add them up
                  $start = $str_len - $offset;//Calculate the starting value
$start = $start >= 0 ? $start : 0;
$count = $end - $start + 1;
      }else {
$str_len = strlen($str);
                  $count = $str_len + $start + 1;//Calculate the length of offset required
$start = 0;
      }
}
while ($i < $count) {
$star .= '*';
$i++;
}
return substr_replace($str, $star, $start, $count);
}
Copy code
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
Copy code
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');
    }

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/871181.htmlTechArticlePHP将部分内容替换成星号 在最近的项目中,会碰到到某人的手机号码隐藏中间几位,身份证号码只显示末尾4位的需求。当时一开始是网上...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template