隐藏用户名、邮箱、手机账号中间字符(支持中文)
隐藏用户名、邮箱、手机账号中间字符(支持中文)/**
* 隐藏用户名、邮箱、手机账号中间字符
* @param string $str 需要处理的字符串
* @param int $bengin开始需要显示的字符个数
* @param int $end 结尾需要显示的字符个数
* @param boolean$email 是否邮箱账号
* @param string $glue 替换符号
* @return string
*/
function hide_account($str, $bengin = 1, $end = 1, $email = false, $glue = "*" ) {
if(!$str){
return "";
}
$encoding = "utf-8";
$email_end = "";
if($email){
if (strpos($str, '@')) {
$email_array = explode("@", $str);
$str = $email_array;
$email_end = $email_array;
}
}
$strlen= mb_strlen($str, $encoding);
//如果只有一个字符
if($strlen == 1){
return $str;
}
if($strlen == 2){
returnmb_substr($str, 0, 1, $encoding).$glue;
}
if($bengin > $strlen){
$bengin = $strlen - 2;
$end = 1;
}
if($end > $strlen){
$bengin = 1;
$end = $strlen - 2;
}
if(($bengin + $end) > $strlen - 1){
$bengin = $strlen - $end - 1;
}
//隐藏长度
$glue_len = $strlen - $bengin - $end;
//首字符
$first_str = mb_substr($str, 0, $bengin, $encoding);
$last_str = mb_substr($str, -$end, null, $encoding);
$hide_str = $first_str . str_repeat($glue, $glue_len) . $last_str;
if($email_end){
return $hide_str . '@' .$email_end;
}
else{
return $hide_str;
}
页:
[1]