In PHP the implementation code of replacing hidden characters of a string part with asterisk *
- 2021-12-21 04:24:08
- OfStack
Sometimes we will encounter such a situation in the development, for example, we need to block out the middle 4 digits when displaying the mobile phone number, and use the "*" number instead, or display the ID number to protect personal information, so it can be realized in the following ways and codes:
Core code
<?php
/**
+----------------------------------------------------------
* Will 1 Partial characters of a string are used * Alternative hiding
+----------------------------------------------------------
* @param string $string String to be converted
* @param int $bengin Starting position, from 0 Start counting when $type=4 Indicates the left reserved length
* @param int $len Need to be converted to * The number of characters in the $type=4 Indicates the right reserved length
* @param int $type Conversion type: 0 Hide from left to right; 1 Hide from right to left; 2 Hide from right to left before splitting from the specified character position; 3 Hide from left to right after being split from the specified character position; 4 Retain the first and last specified strings
* @param string $glue Separator
+----------------------------------------------------------
* @return string Processed string
+----------------------------------------------------------
*/
function hideStr($string, $bengin = 0, $len = 4, $type = 0, $glue = "@") {
if (empty($string))
return false;
$array = array();
if ($type == 0 || $type == 1 || $type == 4) {
$strlen = $length = mb_strlen($string);
while ($strlen) {
$array[] = mb_substr($string, 0, 1, "utf8");
$string = mb_substr($string, 1, $strlen, "utf8");
$strlen = mb_strlen($string);
}
}
if ($type == 0) {
for ($i = $bengin; $i < ($bengin + $len); $i++) {
if (isset($array[$i]))
$array[$i] = "*";
}
$string = implode("", $array);
} else if ($type == 1) {
$array = array_reverse($array);
for ($i = $bengin; $i < ($bengin + $len); $i++) {
if (isset($array[$i]))
$array[$i] = "*";
}
$string = implode("", array_reverse($array));
} else if ($type == 2) {
$array = explode($glue, $string);
$array[0] = hideStr($array[0], $bengin, $len, 1);
$string = implode($glue, $array);
} else if ($type == 3) {
$array = explode($glue, $string);
$array[1] = hideStr($array[1], $bengin, $len, 0);
$string = implode($glue, $array);
} else if ($type == 4) {
$left = $bengin;
$right = $len;
$tem = array();
for ($i = 0; $i < ($length - $right); $i++) {
if (isset($array[$i]))
$tem[] = $i >= $left ? "*" : $array[$i];
}
$array = array_chunk(array_reverse($array), $right);
$array = array_reverse($array[0]);
for ($i = 0; $i < $right; $i++) {
$tem[] = $array[$i];
}
$string = implode("", $tem);
}
return $string;
}
$str = '12345678901';
echo hideStr($str,2,4);
Sometimes you need to consider Chinese substitution, so you can refer to the following implementation method
For the problem of replacing a part of a string with a * sign:
1. Examples:
$username = "linshouyue";
echo substr_replace($username,'****','3','4');
substr_replace () Function
1.1) Parameter 1 is the string to be processed
1.2) Parameter 2 is the replacement symbol used
1.3) Parameter 3/4 is the position of the string to be replaced (replace the last 4 characters from the third character)
However, this function has no problem with English characters/numbers, but it will be very embarrassing when encountering Chinese characters, because the bytes of Chinese characters and English characters are not the same, which can be solved by using 1 method:
/**** Replace Chinese characters
* @ author Month and Month
*/
function substr_cut($user_name){
$strlen = mb_strlen($user_name, 'utf-8');
$firstStr = mb_substr($user_name, 0, 1, 'utf-8');
$lastStr = mb_substr($user_name, -1, 1, 'utf-8');
return $strlen == 2 ? $firstStr . str_repeat('*', mb_strlen($user_name, 'utf-8') - 1) : $firstStr . str_repeat("*", $strlen - 2) . $lastStr;
}
Ok, that's all for this article