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. |