PHP敏感信息脱敏函数

时间:2024-04-16 09:55:13

测试

$name = \'王刚\';
$mobile = \'13817558198\';

$name = desensitize($name,1,1);
$mobile = desensitize($mobile,3,4);

var_dump($name);
var_dump($mobile);

测试结果

王*
138****8198

脱敏函数

function desensitize($string, $start = 0, $length = 0, $re = \'*\'){
    if(empty($string) || empty($length) || empty($re)) return $string;
    $end = $start + $length;
    $strlen = mb_strlen($string);
    $str_arr = array();
    for($i=0; $i<$strlen; $i++) {
        if($i>=$start && $i<$end)
            $str_arr[] = $re;
        else
            $str_arr[] = mb_substr($string, $i, 1);
    }
    return implode(\'\',$str_arr);
}

 

参考:

https://blog.****.net/leyangjun/article/details/81742530