PHP replaces part of the Chengshing number

Source: Internet
Author: User
Tags abs copy count end insert string strlen
In the most recent project, you will encounter someone's mobile phone number hidden in the middle, the ID number shows only the end of the 4-bit demand. At that time at the beginning is the internet search for a while, see someone is using substr_replace this function to replace, I also used this function, but in the use of the time is not very useful.   Substr_replace   First take a look at the syntax of this function:   substr_replace (string,replacement,start,length) parameter description string required. Specify the string to check. Replacement required. Specify the string to insert. Start Required. Specify where the string begins to be replaced.   Positive-replace   negative at start offset-replace   0 at start offset from end of string-replace   charlist at first character in string. Specify how many characters to replace.   Positive-replaced string length   negative-the number of characters replaced from 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, s Tart is starting from 0, the following figure, according to the conditions, the green will be replaced by elements       2, when start is negative, charlist is positive, also very good understanding of       3, When start is positive and charlist is negative, I get it wrong at first.       4, when start is negative and charlist is negative, there is one place to note: If start is negative and length is less than or equal to start, length is 0. This pit is easy to step on       5, charlist is 0, it becomes inserted, not replaced, amount ...       Use down, I am not very comfortable, although the need to meet my needs is still possible, but if the future needs some expansion, playing a very difficult, so think of their own construction, in the future it is convenient to use.       II, self-made asterisk replacement function   REPLACestar ($str, $start, $length = 0) parameter describes STR required. Specify the string to check. Start Required. Specify where the string begins to be replaced.   Positive-replace   negative at start offset-replace   0 at start offset from end of string-replaces   length at the first character in the string. Specify how many characters to replace.   Positive-replaced string length, from left to right   negative-replaced string length, from right to left   0-if start is positive, from start to left to last    -if start is a negative number, starting from start to the right to the last   the previous two parameters are the same as above, the last parameter is different from the above   1, when start and length are positive, and substr_replace performance of the same   2, when start is negative , length is a positive number, as shown by Substr_replace     Substr_replace Replacestar start is positive, length is negative       start is negative, Length is negative          start is positive, length is 0 insert operation  start is negative, length is 0 do insert operation third, source sharing   Copy code public static fun Ction Replacestar ($str, $start, $length = 0) {        $i = 0;         $star = ';         if ($start >= 0) {            if ($length > 0) {  &N Bsp             $STR _len = strlen ($STR);                 $count = $length;                 $start >= $str _len) {//When the start subscript is greater than the length of the string, it does not replace the                     $count = 0;                            }elseif ($length < 0 {                $STR _len = strlen ($STR);           &NB Sp     $count = ABS ($length);                 if ($start >= $str _len) {//when the starting subscript is greater than the length of the string, because it is reversed, it is marked from the bottom of the last character. Start                     $start = $str _len-1;                                 $offset = $start-$count + 1;//beginning subscript minus quantity, calculating offset                 $count = $oFfset >= 0? ABS ($length): ($start + 1);/offset greater than or equal to 0 description no more than leftmost, less than 0 description exceeds leftmost, with start to leftmost length                 $start = $offset >= 0? $offset: 0;//from the leftmost or left position            }else {            & nbsp   $STR _len = strlen ($STR);                 $count = $str _len-$start//Calculate quantity to replace            }        }else {            if ($length > 0) {                $offset = ABS ($start);                 $count = $offset >= $length? $length: $offset//greater than or equal to length not exceeding rightmost            }elseif ($length < 0) {    &NB Sp           $STR _len = strlen ($STR);                 $end = $str _len + $start;//computed offset end value                 $offset = ABS ($start + $length)-1;//calculates offsets, because they are all negative Add up                 $start = $str _len-$offset//Compute start value       &NBSP ;         $start = $start >= 0? $start: 0;                 $count = $end-$start + 1;            }else {                $STR _len = Strle N ($STR);                 $count = $str _len + $start + 1;//calculation needs to offset length       &N Bsp         $start = 0;                     {          while ($i ; $count) {            $star. = ' * ';             $i + +;   & nbsp    }           RETUrn Substr_replace ($str, $star, $start, $count); Copy code is not good at algorithms, here is a very common logic to show that there is no use of any mathematical formula.   1, if ($start >= 0) Here do start is greater than 0 and less than 0 branch   2, in the start of the score, respectively, do longer than 0, less than 0 and equal to 0 three branches   3, the last calculated start, Count and the asterisk string to replace, and the last calculated start and count are positive numbers, using Substr_replace to replace       IV, Unit test   copy code public function Testreplacestar ()     {        $actual = App_util_string::replacestar (' 123456789 ', 3, 2); nbsp       $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); &nbsP       $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 ');    }

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.