Use PHP to replace part of the content with asterisks, PHP content asterisk replace _php tutorial

Source: Internet
Author: User

Use PHP to replace part of the content with asterisks, PHP content asterisk replace


In recent projects, you will encounter someone's mobile phone number hidden in the middle, the ID number only shows the end of the 4-bit demand. At the beginning of the search on the Internet, see someone is replaced with substr_replace this function, I also used this function, but in use is not very useful.

First, Substr_replace
Let's take a look at the syntax of this function:

Copy the Code code as follows: Substr_replace (String,replacement,start,length)

parameter description
string required. Specifies the string to check.
replacement required. Specifies the string to insert.
Start

Necessary. Specifies where the string begins to be replaced.

positive-in EM style= "font-style:normal; font-size:12px ">start offset start replace

negative-at the end of the string start Offset starts replacing

0-Start the substitution at the first character in the string

Charlist

Optional. Specifies the number of characters to replace.

Positive number-The length of the string being replaced

Negative-the number of characters to be replaced starting at the end of the string

0-Insert rather than replace

1, when start and charlist are positive, very good understanding, also very symbolic of human logic, start is starting from 0, such as, according to the conditions, the green will be replaced by the element

2, when start is negative, charlist is positive, also very good understanding

3, when start is a positive number, charlist is negative, this I start to understand wrong

4, when start is negative, charlist is negative, there is a place to note is: If start is negative and length is less than or equal to start, then length is 0. This hole is pretty easy to step on.

5, when the charlist is 0, it becomes inserted, not replaced, the amount ...

Use down, I am not very comfortable, although said to meet my current needs or can, but if the future needs some expansion, it is very difficult to play, so think of their own construction of a, the future use is also convenient.

Second, the self-made asterisk replacement function

Copy the Code code as follows: Replacestar ($str, $start, $length = 0)

The previous two parameters are the same as above, and the last parameter is different from the previous one

1, when start and length are positive, the same as the Substr_replace performance

2, when start is negative, length is positive, and the same as Substr_replace performance

Iii. Sharing of source 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 >= $str _len) {//when the starting subscript is greater than the length of the string, do not replace $count = 0;    }}elseif ($length < 0) {$str _len = strlen ($STR);    $count = ABS ($length);    if ($start >= $str _len) {//when the starting subscript is greater than the length of the string, because it is reversed, start with the subscript of the last character $start = $str _len-1; } $offset = $start-$count + 1;//start subscript minus quantity, calculate offset $count = $offset >= 0? ABS ($length): ($start + 1);//offset greater than or equal to 0 indicates no more than the leftmost, less than 0 of the description exceeds the leftmost, using the starting point to the leftmost length $start = $offset >= 0?    $offset: 0;//starts 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;//greater than or equal to length does not exceed the rightmost}elseif ($length < 0) {$str _len = strlen ($STR); $end = $str _len + $start;//Calculates the end value of the offset $offset = ABS ($start + $length)-1;//calculate the offset, because it is negative, add 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;//calculates the length of the offset required $start = 0;   }} while ($i < $count) {$star. = ' * ';  $i + +; } return Substr_replace ($str, $star, $start, $count);}

Not good at algorithms, here is a very common logic to show, no use of what mathematical formula.

1, if ($start >= 0) Here do start is greater than or equal to 0 and less than 0 branches

2, in the division of Start, and then do the length of more than 0, less than 0 and equal to 0 three branches

3, finally calculates the start, count and the asterisk string to replace, the last calculated start and count are positive, using substr_replace to replace

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 the whole content of this article, I hope that everyone's study has helped.

http://www.bkjia.com/PHPjc/1040810.html www.bkjia.com true http://www.bkjia.com/PHPjc/1040810.html techarticle using PHP to replace part of the content with asterisks, PHP content asterisks replaced in the recent project, will encounter someone's mobile phone number hidden in the middle, the ID number only shows the end of the 4-bit need ...

  • Related Article

    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.