In php, replace the characters at the specified position in the string with asterisks. our function can be used to implement substr, preg_replace, substr_replace, and so on. I will respectively assign these three functions
In php, replace the characters at the specified position in the string with asterisks. our function can be used to implement substr, preg_replace, substr_replace, and so on. below we will introduce one instance for the three functions respectively, we mainly talk about the phone number and ID card.
Replace the phone number string with the asterisk code:
-
- $ Str = "15832818835 ";
- Echo substr ($ str,). '*****'. substr ($ str, 8, strlen ($ str); // retain the first and last three digits
- ?>
- // Or use regular expressions
-
- $ S = 'manager Wang of www .111cn.net: 13999312365 manager Li: 13588958741 ';
- $ S = preg_replace ('# (d {3}) d {5} (d {3 })#', '$ {1} ***** $ {2}', $ s );
- Echo $ s;
- // Manager Wang: 139 ***** 365 manager Li: 135 ***** 741
- ?>
Use the following code to replace the character in the middle of a string with an asterisk:
- Function half_replace ($ str ){
- $ Len = strlen ($ str)/2;
- Return substr_replace ($ str, str_repeat ('*', $ len), ceil ($ len)/2), $ len );
- }
- Echo half_replace ('test'), "n", half_replace ('tests'), "n", half_replace ('exceptions ');
Php id card number, the code is as follows:
- Echo strlen ($ idcard) = 15? Substr_replace ($ idcard, "*****", 8, 4) :( strlen ($ idcard) = 18? Substr_replace ($ idcard, "*****",): "111cn.net indicates that the number of digits of the ID card is abnormal! ");