136 lines
4.0 KiB
PHP
136 lines
4.0 KiB
PHP
<?php
|
||
|
||
|
||
namespace App\Utils;
|
||
|
||
|
||
class Mask
|
||
{
|
||
/**
|
||
* 用户名掩码
|
||
* @param string $str 姓名 英文、中文、中英文混合的、中英文字符混合
|
||
* @param int $type 类型1:首字母和末尾保留,中间用*号代替 2:首字母保留,后两位用*号代替
|
||
* @return mixed
|
||
*/
|
||
public static function maskNameStr(string $str = '', int $type = 1): mixed
|
||
{
|
||
if (empty($str) ){
|
||
return $str;
|
||
}
|
||
|
||
$str = mb_convert_encoding( $str , 'UTF-8', 'auto' );
|
||
//判断是否包含中文字符
|
||
if(preg_match("/[\x{4e00}-\x{9fa5}]+/u", $str)) {
|
||
//按照中文字符计算长度
|
||
$len = mb_strlen($str, 'UTF-8');
|
||
//echo '中文';
|
||
if($len >= 3){
|
||
if ($type == 1){
|
||
//三个字符或三个字符以上掐头取尾,中间用*代替
|
||
$str = mb_substr($str, 0, 1, 'UTF-8') . '*' . mb_substr($str, -1, 1, 'UTF-8');
|
||
}else{
|
||
$str = mb_substr($str, 0, 1, 'UTF-8') . '**';
|
||
}
|
||
} elseif($len == 2) {
|
||
//两个字符
|
||
$str = mb_substr($str, 0, 1, 'UTF-8') . '*';
|
||
}
|
||
} elseif(preg_match("/[A-Za-z]/", $str)) {
|
||
//按照英文字串计算长度
|
||
$len = mb_strlen($str);
|
||
//echo 'English';
|
||
if($len >= 3) {
|
||
if ($type == 1){
|
||
//三个字符或三个字符以上掐头取尾,中间用*代替
|
||
$str = mb_substr($str, 0, 1) . '*' . mb_substr($str, -1);
|
||
}else{
|
||
$str = mb_substr($str, 0, 1) . '**';
|
||
}
|
||
} elseif($len == 2) {
|
||
//两个字符
|
||
$str = mb_substr($str, 0, 1) . '*';
|
||
}
|
||
}
|
||
return $str;
|
||
}
|
||
/*
|
||
* 手机号、固话加密
|
||
* 示例:
|
||
* 固话:0510-89754815 0510-8****815
|
||
* 手机号:18221234158 18*******58
|
||
* */
|
||
public static function maskPhoneStr($phone)
|
||
{
|
||
if (empty($phone)){
|
||
return $phone;
|
||
}
|
||
|
||
$IsWhat = preg_match('/(0[0-9]{2,3}[\-]?[2-9][0-9]{6,7}[\-]?[0-9]?)/i',$phone); //固定电话
|
||
if($IsWhat == 1){
|
||
return preg_replace('/(0[0-9]{2,3}[\-]?[2-9])[0-9]{3,4}([0-9]{3}[\-]?[0-9]?)/i','$1****$2',$phone);
|
||
}else{
|
||
return preg_replace('/(1[0-9]{1})[0-9]{7}([0-9]{2})/i','$1*******$2',$phone);
|
||
}
|
||
}
|
||
/*
|
||
* 地址中夹带数字加密
|
||
* 示例:
|
||
* 北京市124Ff:北京市***Ff
|
||
* */
|
||
public static function maskAddressStr($address)
|
||
{
|
||
if (empty($address)){
|
||
return $address;
|
||
}
|
||
|
||
$address = mb_convert_encoding( $address , 'UTF-8', 'auto' );
|
||
$pattern = '/[0-9]/';
|
||
if(preg_match_all($pattern, $address, $match)){
|
||
return str_replace($match[0],'*',$address);
|
||
}else{
|
||
return $address;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 身份证掩码
|
||
* 示例:
|
||
* 372929199610075411:372929****5411
|
||
* @param string $card_num
|
||
* @return string
|
||
*/
|
||
public static function maskIdCard(string $card_num): string
|
||
{
|
||
if (empty($card_num)){
|
||
return $card_num;
|
||
}
|
||
|
||
$result = preg_replace('/(\\w{6})(\\w+)(\\w{4})/','$1****$3',$card_num);
|
||
if (empty($result)){
|
||
return $card_num;
|
||
}
|
||
|
||
return $result;
|
||
}
|
||
|
||
/**
|
||
* 银行卡掩码
|
||
* 示例:
|
||
* 6228480038716690979:6228****0979
|
||
* @param string $card_num
|
||
* @return string
|
||
*/
|
||
public static function maskBankCard(string $card_num): string
|
||
{
|
||
if (empty($card_num)){
|
||
return $card_num;
|
||
}
|
||
|
||
$result = preg_replace('/^(.{4})(.*)(.{4})$/','${1}****${3}',$card_num);
|
||
if (empty($result)){
|
||
return $card_num;
|
||
}
|
||
|
||
return $result;
|
||
}
|
||
} |