Use PHP to replace part of the content with an asterisk

Source: Internet
Author: User
This article uses php to hide the middle digit of a mobile phone number. the ID card number only shows the last four digits. For more information, see the latest project, you may need to hide the middle digit of a person's mobile phone number. the ID card number must display only the last four digits. At that time, I searched the internet and found someone replaced it with the substr_replace function. I also used this function later, but it was not very easy to use.

I. substr_replace
Let's take a look at the syntax of this function:

The code is as follows:

Substr_replace (string, replacement, start, length)

The code is as follows:

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

The preceding two parameters are the same as those above, and the final parameters are different from those above.

1. When start and length are both positive numbers, the performance is the same as that of substr_replace.

2. when start is negative and length is positive, it performs the same as substr_replace.

III. 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) {// when the start subscript is greater than the string length, $ count = 0 ;}} elseif ($ length <0) is not replaced) {$ str_len = strlen ($ str); $ count = abs ($ length); if ($ start> = $ str_len) {// when the start subscript is greater than the string length, because it is reverse, it starts from the subscript of the last character $ start = $ str_len-1;} $ offset = $ start -$ Count + 1; // The starting subscript minus the number, calculates the offset $ count = $ offset> = 0? Abs ($ length): ($ start + 1); // if the offset is greater than or equal to 0, the value does not exceed the leftmost value. if the offset is smaller than 0, the value exceeds the leftmost value, the length from the start point to the left is $ start = $ offset> = 0? $ Offset: 0; // start from the leftmost or left position} else {$ str_len = strlen ($ str); $ count = $ str_len-$ start; // calculate the quantity to replace} else {if ($ length> 0) {$ offset = abs ($ start); $ count = $ offset >=$ length? $ Length: $ offset; // when the length is 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 all the values are negative, they add up $ start = $ str_len-$ offset; // calculates the starting point value $ start = $ start> = 0? $ Start: 0; $ count = $ end-$ start + 1;} else {$ str_len = strlen ($ str); $ count = $ str_len + $ start + 1; // calculate the length of the offset $ start = 0;} while ($ I <$ count) {$ star. = '*'; $ I ++;} return substr_replace ($ str, $ star, $ start, $ count );}

If you are not good at algorithms, you can use common logic to demonstrate them. you cannot use mathematical formulas.

1. if ($ start> = 0) here, start is the branch where start is greater than or equal to 0 and less than 0.

2. in the start part, do the three branches with the length greater than 0, less than 0, and equal to 0 respectively.

3. finally, start, count, and asterisks to be replaced are calculated. the start and count calculated are all positive numbers, and substr_replace is used for replacement.

IV. Unit testing

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 all the content of this article, hoping to help you learn.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.