Use PHP to replace the _ php instance 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)

Parameters Description
String Required. Specifies the string to be checked.
Replacement Required. Specifies the string to be inserted.
Start

Required. Specifies where the string starts replacement.

Positive number-inStartOffset start to replace

Negative number-the number at the end of the stringStartOffset start to replace

0-start replacement at the first character in the string

Charlist

Optional. Specifies the number of characters to replace.

Positive number-length of the string to be replaced

Negative number-Number of replaced characters starting from the end of the string

0-insert instead of replace

1. When start and charlist are both positive numbers, it is easy to understand, and it is also a symbolic human logic. start starts from 0. For example, according to the condition, green will be the elements to be replaced

2. When start is negative and charlist is positive, it is quite understandable.

3. When start is a positive number and charlist is a negative number, I initially understood it as wrong.

4. When start is negative and charlist is negative, note that if start is negative and length is less than or equal to start, length is 0. This pitfall is easy to step on.

5. When charlist is 0, it is inserted instead of being replaced...

I don't feel very comfortable with using it. Although it is okay to satisfy my current needs, it will be very difficult if I need some extensions in the future, so I thought of building one by myself, which will be easy to use in the future.

2. Self-made asterisk replacement function

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.