测试
$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); }
参考: