using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace common
{
public class PhoneHelper
{
#region 超过8个数字的数据替换成*
///
/// 超过8个数字的数据替换成*
///
///
///
public static string Replace8Number(object txt)
{
if (txt == null)
{
return "";
}
if (string.IsNullOrEmpty(txt.ToString()))
{
return "";
}
var content = txt.ToString();
//普通数字
string numReg = @"\d+";
var result = Regex.Matches(content, numReg).Cast().Select(t => t.Value).ToList();
if (result != null && result.Count > 0)
{
foreach (var s in result)
{
if (s.Length > 7)
{
content = content.Replace(s, GetHideNumber(s));
}
}
}
//下标数字
numReg = @"[₁,₂,₃,₄,₅,₆,₇,₈,₉,₀]{8,}";
result = Regex.Matches(content, numReg).Cast().Select(t => t.Value).ToList();
if (result != null && result.Count > 0)
{
foreach (var s in result)
{
if (s.Length > 7)
{
content = content.Replace(s, GetHideDownNumber(s));
}
}
}
//上标数字
numReg = @"[¹,²,³,⁴,⁵,⁶,⁷,⁸,⁹,⁰]{8,}";
result = Regex.Matches(content, numReg).Cast().Select(t => t.Value).ToList();
if (result != null && result.Count > 0)
{
foreach (var s in result)
{
if (s.Length > 7)
{
content = content.Replace(s, GetHideUpNumber(s));
}
}
}
//全角数字
numReg = @"[1,2,3,4,5,6,7,8,9,0]{8,}";
result = Regex.Matches(content, numReg).Cast().Select(t => t.Value).ToList();
if (result != null && result.Count > 0)
{
foreach (var s in result)
{
if (s.Length > 7)
{
content = content.Replace(s, GetHideRoundNumber(s));
}
}
}
return content;
}
//全角数字屏蔽
public static string GetHideRoundNumber(string phoneNo)
{
string rexstr = "";
string xinghao = "";
if (phoneNo.Length == 8)
{
rexstr = @"([1,2,3,4,5,6,7,8,9,0]{2})([1,2,3,4,5,6,7,8,9,0]{4})([1,2,3,4,5,6,7,8,9,0]{2})";
xinghao = "****";
}
else if (phoneNo.Length == 9)
{
rexstr = @"([1,2,3,4,5,6,7,8,9,0]{2})([1,2,3,4,5,6,7,8,9,0]{5})([1,2,3,4,5,6,7,8,9,0]{2})";
xinghao = "*****";
}
else if (phoneNo.Length == 10)
{
rexstr = @"([1,2,3,4,5,6,7,8,9,0]{2})([1,2,3,4,5,6,7,8,9,0]{5})([1,2,3,4,5,6,7,8,9,0]{3})";
xinghao = "*****";
}
else
{
rexstr = @"([1,2,3,4,5,6,7,8,9,0]{3})([1,2,3,4,5,6,7,8,9,0]{" + (phoneNo.Length - 6) + @"})([1,2,3,4,5,6,7,8,9,0]{3})";
for (int i = 0; i < (phoneNo.Length - 6); i++)
{
xinghao += "*";
}
}
Regex re = new Regex(rexstr, RegexOptions.None);
phoneNo = re.Replace(phoneNo, "$1" + xinghao + "$3");
return phoneNo;
}
//上标数字屏蔽
public static string GetHideUpNumber(string phoneNo)
{
string rexstr = "";
string xinghao = "";
if (phoneNo.Length == 8)
{
rexstr = @"([¹,²,³,⁴,⁵,⁶,⁷,⁸,⁹,⁰,]{2})([¹,²,³,⁴,⁵,⁶,⁷,⁸,⁹,⁰,]{4})([¹,²,³,⁴,⁵,⁶,⁷,⁸,⁹,⁰,]{2})";
xinghao = "****";
}
else if (phoneNo.Length == 9)
{
rexstr = @"([¹,²,³,⁴,⁵,⁶,⁷,⁸,⁹,⁰,]{2})([¹,²,³,⁴,⁵,⁶,⁷,⁸,⁹,⁰,]{5})([¹,²,³,⁴,⁵,⁶,⁷,⁸,⁹,⁰,]{2})";
xinghao = "*****";
}
else if (phoneNo.Length == 10)
{
rexstr = @"([¹,²,³,⁴,⁵,⁶,⁷,⁸,⁹,⁰,]{2})([¹,²,³,⁴,⁵,⁶,⁷,⁸,⁹,⁰,]{5})([¹,²,³,⁴,⁵,⁶,⁷,⁸,⁹,⁰,]{3})";
xinghao = "*****";
}
else
{
rexstr = @"([¹,²,³,⁴,⁵,⁶,⁷,⁸,⁹,⁰,]{3})([¹,²,³,⁴,⁵,⁶,⁷,⁸,⁹,⁰,]{" + (phoneNo.Length - 6) + @"})([¹,²,³,⁴,⁵,⁶,⁷,⁸,⁹,⁰,]{3})";
for (int i = 0; i < (phoneNo.Length - 6); i++)
{
xinghao += "*";
}
}
Regex re = new Regex(rexstr, RegexOptions.None);
phoneNo = re.Replace(phoneNo, "$1" + xinghao + "$3");
return phoneNo;
}
//下标数字屏蔽
public static string GetHideDownNumber(string phoneNo)
{
string rexstr = "";
string xinghao = "";
if (phoneNo.Length == 8)
{
rexstr = @"([₁,₂,₃,₄,₅,₆,₇,₈,₉,₀]{2})([₁,₂,₃,₄,₅,₆,₇,₈,₉,₀]{4})([₁,₂,₃,₄,₅,₆,₇,₈,₉,₀]{2})";
xinghao = "****";
}
else if (phoneNo.Length == 9)
{
rexstr = @"([₁,₂,₃,₄,₅,₆,₇,₈,₉,₀]{2})([₁,₂,₃,₄,₅,₆,₇,₈,₉,₀]{5})([₁,₂,₃,₄,₅,₆,₇,₈,₉,₀]{2})";
xinghao = "*****";
}
else if (phoneNo.Length == 10)
{
rexstr = @"([₁,₂,₃,₄,₅,₆,₇,₈,₉,₀]{2})([₁,₂,₃,₄,₅,₆,₇,₈,₉,₀]{5})([₁,₂,₃,₄,₅,₆,₇,₈,₉,₀]{3})";
xinghao = "*****";
}
else
{
rexstr = @"([₁,₂,₃,₄,₅,₆,₇,₈,₉,₀]{3})([₁,₂,₃,₄,₅,₆,₇,₈,₉,₀]{" + (phoneNo.Length - 6) + @"})([₁,₂,₃,₄,₅,₆,₇,₈,₉,₀]{3})";
for (int i = 0; i < (phoneNo.Length - 6); i++)
{
xinghao += "*";
}
}
Regex re = new Regex(rexstr, RegexOptions.None);
phoneNo = re.Replace(phoneNo, "$1" + xinghao + "$3");
return phoneNo;
}
//数字屏蔽
public static string GetHideNumber(string phoneNo)
{
string rexstr = "";
string xinghao = "";
if (phoneNo.Length == 8)
{
rexstr = @"(\d{2})(\d{4})(\d{2})";
xinghao = "****";
}
else if (phoneNo.Length == 9)
{
rexstr = @"(\d{2})(\d{5})(\d{2})";
xinghao = "*****";
}
else if (phoneNo.Length == 10)
{
rexstr = @"(\d{2})(\d{5})(\d{3})";
xinghao = "*****";
}
else
{
rexstr = @"(\d{3})(\d{" + (phoneNo.Length - 6) + @"})(\d{3})";
for (int i = 0; i < (phoneNo.Length - 6); i++)
{
xinghao += "*";
}
}
Regex re = new Regex(rexstr, RegexOptions.None);
phoneNo = re.Replace(phoneNo, "$1" + xinghao + "$3");
return phoneNo;
}
#endregion
///
/// 格式化以手机号码作为用户名的username
///
///
///
public static string FormatPhoneUserName(string username)
{
string newUsername = GetFormatPhoneUserName(username);//手机号码格式化
return newUsername;
}
///
/// 格式化以手机号码作为用户名的username(存在多个用户名)
///
///
///
public static string FormatPhoneMoreUserName(string username)
{
string newUsername = GetFormatPhoneUserName(username);//手机号码格式化
if (string.IsNullOrEmpty(newUsername))
return newUsername;
string userNames = string.Empty;
foreach (var item in newUsername.Split(','))
{
userNames += FormatUserName(item) + ",";//用户名格式化
}
userNames = userNames.Substring(0, userNames.Length - 1);
return userNames;
}
///
/// 格式化以手机号码作为用户名的username
///
/// 需要改动的用户名
///
///
public static string FormatPhoneUserNameContent(string username)
{
string newUsername = GetFormatPhoneUserName(username);//手机号码格式化
return newUsername;
}
///
/// 用户名
///
///
///
public static string FormatUserName(string userName)
{
/*
用户名屏蔽规则:
1、手机号形式的按原处理规则中间几位屏蔽;
2、否则看用户名长度,小于等于5位的,第一位不屏蔽,后面的屏蔽,比如“test1”,处理后为“t****”;
3、5位以上的,用户名长度除2向下取整,再减1做为起始值,从起始值开始后面4位用*号;比如“test123”,处理后为“te****3”
*/
if (string.IsNullOrEmpty(userName))
return "";
if (userName == "未设置")
return userName;
string newUserName = userName;
//判断 是否已经在手机号屏蔽的时候屏蔽过一次
if (userName.IndexOf("*****") > -1)
{
return newUserName;
}
int nameLth = newUserName.Length;
if (nameLth <= 5)
{
newUserName = newUserName.Substring(0, 1) + GetAsterisk(nameLth - 1);
}
else
{
int startIndex = nameLth / 2;
startIndex = startIndex - 1;
newUserName = newUserName.Substring(0, startIndex) + "****" + newUserName.Substring(startIndex + 4, nameLth - startIndex - 4);
}
return newUserName;
}
///
/// 格式化以手机号码作为用户名的username
///
///
///
private static string GetFormatPhoneUserName(string username)
{
string newUsername = username;
if (string.IsNullOrWhiteSpace(newUsername))
return newUsername;
while (ContainMobile(newUsername))
{
string phone = GetMobile(newUsername);
if (string.IsNullOrWhiteSpace(phone))
break;
newUsername = newUsername.Replace(phone, (phone.Substring(0, 3) + "*****" + phone.Substring(8, 3)));
}
return newUsername;
}
///
/// 获取星号
///
/// 需要返回的星号数量
///
private static string GetAsterisk(int number)
{
string xingHao = string.Empty;
if (number == 0)
return xingHao;
for (int i = 0; i < number; i++)
{
xingHao += "*";
}
return xingHao;
}
public static bool ContainMobile(string number)
{
string rgs = @".*(11|12|13|14|15|16|17|18|19)\d{9}.*";
Regex reg = new Regex(rgs);
return reg.IsMatch(number);
}
///
/// 从文本中返回号码字符串
///
///
///
public static string GetMobile(string txt)
{
string rgs = @"(11|12|13|14|15|16|17|18|19)\d{9}";
string result = Regex.Match(txt, rgs).Value;
return result;
}
}
}