PHP replaces Part of the content with a star number

Source: Internet
Author: User

PHP replaces Part of the content with a star number
In a recent 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 first let's take a look at the syntax of this function: The substr_replace (string, replacement, start, length) parameter description string is required. Specifies the string to be checked. Replacement is required. Specifies the string to be inserted. Start is required. Specifies where the string starts replacement. Positive number-replace negative number at the start offset-replace 0 at the start offset at the end of the string-replace charlist at the first character in the string. Optional. Specifies the number of characters to replace. Positive-negative number of replaced strings-Number of replaced characters starting from the end of the string 0-insert rather than replace 1. It is easy to understand when start and charlist are both positive, it is also a symbolic human logic. start starts from 0. For example, according to the condition, the green element will be replaced. 2. When start is negative, when charlist is a positive number, it is quite understandable. 3. When start is a positive number and charlist is a negative number, I first understood it as wrong. 4. When start is a negative number, when charlist is a negative number, note that if start is a negative number and the length is less than or equal to start, the length is 0. This pitfall is easy to step on. 5. When charlist is 0, it will become inserted instead of 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. The replaceStar ($ str, $ start, $ length = 0) parameter description of the self-made asterisk replacement function is required. Specifies the string to be checked. Start is required. Specifies where the string starts replacement. Positive number-replace negative number at the start offset-replace 0 at the start offset at the end of the string-replace length at the first character in the string. Optional. Specifies the number of characters to replace. Positive number-length of the string to be replaced, from left to right negative number-length of the string to be replaced, from right to left 0-If start is positive, start from start to left to the end-If start is negative, the two parameters from start to right to the end are the same as those above, the final parameter is different from the preceding one. When start and length are both positive numbers, they are the same as substr_replace. 2. When start is negative, length is positive, and substr_replace is the same as substr_replacereplaceStarstart, the length is negative. start is negative. start is positive. If the length is 0, the insert operation start is negative, the length is 0 for insert operation 3. source code sharing and replication code public static function replaceStar ($ str, $ start, $ length = 0) {$ I = 0; $ star = ''; if ($ st Art >=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; // calculates the offset of the starting subscript minus the number. $ 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);} copying code is not good at algorithms, here we will use a very general logic to demonstrate it, and we will not use any mathematical formulas. 1. if ($ start> = 0) here do start branches greater than or equal to 0 and less than 0. 2. In the start part, do length greater than 0, respectively, three branches smaller than 0 and equal to 0. 3. Finally, start, count, and the asterisk string to be replaced are calculated. The start and count calculated are all positive numbers, replace with substr_replace. 4. Run the unit test to copy the code public function testReplaceStar () {$ actual = App_Util_String: replaceStar ('20140901', 3, 2 ); $ this-> assertEquals ($ actual, '1970*123 '); $ actual = App_Util_String: replaceStar ('20140901', 9 ); $ this-> assertEquals ($ actual, '000000'); $ actual = App_Util_String: replaceStar ('000000', 9, 2); $ this-> assertEquals ($ actual, '20140901'); $ actual = App_Util_String: replaceStar ('20140901', 9,-9); $ this-> assertEquals ($ actual, '*********'); $ actual = App_Util_String: replaceStar ('20140901', 9,-10); $ this-> assertEquals ($ actual, '*********'); $ actual = App_Util_String: replaceStar ('20140901', 9,-11); $ this-> assertEquals ($ actual, '*********'); $ actual = App_Util_String: replaceStar ('20140901', 3); $ this-> assertEquals ($ actual, '1970 ****** '); $ actual = App_Util_String: replaceStar ('20140901', 0); $ this-> assertEquals ($ actual, '*********'); $ actual = App_Util_String: replaceStar ('20140901', 0, 2); $ this-> assertEquals ($ actual, '** 3456789'); $ actual = App_Util_String: replaceStar ('000000', 3,-3); $ this-> assertEquals ($ actual, '1 *** 56789 '); $ actual = App_Util_String: replaceStar ('000000', 1,-5); $ this-> assertEquals ($ actual, '** 3456789'); $ actual = App_Util_String: replaceStar ('000000', 3,-3); $ this-> assertEquals ($ actual, '1 *** 56789 '); $ actual = App_Util_String: replaceStar ('20140901',-3, 2); $ this-> assertEquals ($ actual, '1970 ** 9'); $ actual = App_Util_String: replaceStar ('20140901',-3, 5); $ this-> assertEquals ($ actual, '1970 *** '); $ actual = App_Util_String: replaceStar ('20140901',-1, 2); $ this-> assertEquals ($ actual, '1970 * '); $ actual = App_Util_String: replaceStar ('20140901',-1,-2); $ this-> assertEquals ($ actual, '1970 ** '); $ actual = App_Util_String: replaceStar ('20140901',-4,-7); $ this-> assertEquals ($ actual, '***** 789'); $ actual = App_Util_String: replaceStar ('20140901',-1,-3); $ this-> assertEquals ($ actual, '1970 *** '); $ actual = App_Util_String: replaceStar ('20140901',-1); $ this-> assertEquals ($ actual, '*********'); $ actual = App_Util_String: replaceStar ('20140901',-2); $ this-> assertEquals ($ actual, '******** 9'); $ actual = App_Util_String: replaceStar ('20140901',-9); $ this-> assertEquals ($ actual, '* 23456789'); $ actual = App_Util_String: replaceStar ('000000',-10); $ this-> assertEquals ($ actual, '20140901 '); $ actual = App_Util_String: replaceStar ('000000',-10,-2); $ this-> assertEquals ($ actual, '000000 ');}

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.