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.
First, Substr_replace
Let's take a look at the syntax of this function:
Copy Code code as follows:
Substr_replace (String,replacement,start,length)
Parameters |
Describe |
string |
necessary. Specify the string to check. |
Replacement |
Necessary. Specify the string to insert. |
Start |
Necessary. Specify where the string begins to be replaced. Positive number-replace at start offset Negative number-replaces the start offset at The end of the string 0-Start the substitution at the first character in the string |
Charlist |
Optional. Specify how many characters to replace. Positive number-The length of the string being replaced Negative numbers-the number of replaced characters starting at the end of the string 0-Insert rather than replace |
1, when start and charlist are positive, very good understanding, but also very symbolic of human logic, start is starting from 0, the following figure, according to the conditions, green will be replaced by the elements
2, when the start is negative, charlist is positive, it is also very good to understand
3, when start is a positive number, charlist is negative, this I started to understand the wrong
4. When start is negative and charlist is negative, there is one place to note: If start is a negative number and length is less than or equal to start, length is 0. This is a very easy pit 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 needs now is still possible, but if the future needs some expansion, playing a very difficult, so think of their own construction, the future use is convenient.
Second, homemade asterisk replacement function
Copy Code code as follows:
Replacestar ($str, $start, $length = 0)
The previous two parameters are the same as above, and the last argument 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 positive, and substr_replace performance of the same
Third, source 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 starting subscript is greater than the length of the string, it is not replaced $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, it is reversed, starting at the subscript of the last character $start = $str _len-1; $offset = $start-$count + 1;//beginning subscript minus quantity, calculating offset $count = $offset >= 0? ABS ($length): ($start + 1);/offset is greater than or equal to 0 the description does not exceed the leftmost, less than 0 the description exceeds the leftmost, with the starting point to the leftmost length $start = $offset >= 0?
$offset: 0;//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 the 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;//calculates offsets, which are added by negative numbers $start = $str _len-$offset = The starting value is calculated $start = $start >= 0?
$start: 0;
$count = $end-$start + 1;
}else {$str _len = strlen ($STR);
$count = $str _len + $start + 1;//compute the length to be offset $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 that there is no use of any mathematical formula.
1, if ($start >= 0) Here do start a branch greater than or equal to 0 and less than 0
2, in the start of the points, and then do length greater than 0, less than 0 and equal to 0 of the three branches
3, the final calculation of start, count and to replace the asterisk string, the last calculated start and count are positive numbers, using Substr_replace to do the replacement
Four, Unit test
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 entire content of this article, I hope to help you learn.