1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5:
6: namespace AnkeEdu.Tools
7: {
8:
9: /// <summary>
10: /// 电话号码帮助类
11: /// author:Andrew.He
12: /// 给定一个电话号码,返回电话号码号段所属网络
13: /// </summary>
14: public class PhoneHelper
15: {
16: #region mobileRegex 检测号码的正则表达式
17: /// <summary>
18: /// 检测号码的正则表达式
19: /// </summary>
20: private const string mobileRegex = @"(86)?0?1\d{10}";
21: #endregion
22:
23: #region keyValues 手机号段集合,PhoneType.Mobile 移动 PhoneType.Unicom 联通 PhoneType.Telecom 电信
24: /// <summary>
25: /// 手机号段集合,PhoneType.Mobile 移动 PhoneType.Unicom 联通 PhoneType.Telecom 电信
26: /// </summary>
27: private static List<PhoneKeyValue> keyValues = new List<PhoneKeyValue>()
28: {
29: new PhoneKeyValue("1340",PhoneType.Mobile),
30: new PhoneKeyValue("1341",PhoneType.Mobile),
31: new PhoneKeyValue("1342",PhoneType.Mobile),
32: new PhoneKeyValue("1343",PhoneType.Mobile),
33: new PhoneKeyValue("1344",PhoneType.Mobile),
34: new PhoneKeyValue("1345",PhoneType.Mobile),
35: new PhoneKeyValue("1346",PhoneType.Mobile),
36: new PhoneKeyValue("1347",PhoneType.Mobile),
37: new PhoneKeyValue("1348",PhoneType.Mobile),
38: new PhoneKeyValue("135",PhoneType.Mobile),
39: new PhoneKeyValue("136",PhoneType.Mobile),
40: new PhoneKeyValue("137",PhoneType.Mobile),
41: new PhoneKeyValue("138",PhoneType.Mobile),
42: new PhoneKeyValue("139",PhoneType.Mobile),
43: new PhoneKeyValue("147",PhoneType.Mobile),
44: new PhoneKeyValue("150",PhoneType.Mobile),
45: new PhoneKeyValue("151",PhoneType.Mobile),
46: new PhoneKeyValue("152",PhoneType.Mobile),
47: new PhoneKeyValue("157",PhoneType.Mobile),
48: new PhoneKeyValue("158",PhoneType.Mobile),
49: new PhoneKeyValue("159",PhoneType.Mobile),
50: new PhoneKeyValue("182",PhoneType.Mobile),
51: new PhoneKeyValue("183",PhoneType.Mobile),
52: new PhoneKeyValue("184",PhoneType.Mobile),
53: new PhoneKeyValue("187",PhoneType.Mobile),
54: new PhoneKeyValue("188",PhoneType.Mobile),
55: new PhoneKeyValue("130",PhoneType.Unicom),
56: new PhoneKeyValue("131",PhoneType.Unicom),
57: new PhoneKeyValue("132",PhoneType.Unicom),
58: new PhoneKeyValue("145",PhoneType.Unicom),
59: new PhoneKeyValue("155",PhoneType.Unicom),
60: new PhoneKeyValue("156",PhoneType.Unicom),
61: new PhoneKeyValue("185",PhoneType.Unicom),
62: new PhoneKeyValue("186",PhoneType.Unicom),
63: new PhoneKeyValue("133",PhoneType.Telecom),
64: new PhoneKeyValue("1349",PhoneType.Telecom),
65: new PhoneKeyValue("153",PhoneType.Telecom),
66: new PhoneKeyValue("180",PhoneType.Telecom),
67: new PhoneKeyValue("181",PhoneType.Telecom),
68: new PhoneKeyValue("189",PhoneType.Telecom)
69: };
70: #endregion
71:
72: #region GetPhoneType 检测手机号码是否为对应的手机类别
73: /// <summary>
74: /// 检测手机号码是否为对应的手机类别
75: /// </summary>
76: /// <param name="phoneNo"></param>
77: /// <returns></returns>
78: public static PhoneType GetPhoneType(string phoneNo)
79: {
80: if (!IsPhoneNo(phoneNo))
81: {
82: return PhoneType.NotPhoneNo;
83: }
84:
85: var model = keyValues.Where(x => phoneNo.StartsWith(x.key)).FirstOrDefault();
86: if (model != null)
87: {
88: return model.PhoneTypeValue;
89: }
90:
91: return PhoneType.Other;
92: }
93: #endregion
94:
95: #region IsPhoneNo 检测号码是否为手机号码
96: /// <summary>
97: /// 检测号码是否为手机号码
98: /// </summary>
99: /// <param name="phoneNo"></param>
100: /// <returns></returns>
101: private static bool IsPhoneNo(string phoneNo)
102: {
103: if (string.IsNullOrEmpty(phoneNo))
104: {
105: return false;
106: }
107:
108: return System.Text.RegularExpressions.Regex.Match(phoneNo, mobileRegex,
109: System.Text.RegularExpressions.RegexOptions.IgnoreCase |
110: System.Text.RegularExpressions.RegexOptions.ECMAScript).Success;
111: }
112: #endregion
113: }
114:
115: #region PhoneKeyValue 手机号段实体
116: /// <summary>
117: /// 手机号段实体
118: /// </summary>
119: public class PhoneKeyValue
120: {
121: public PhoneKeyValue(string key,PhoneType type)
122: {
123: this.key = key;
124: this.PhoneTypeValue = type;
125: }
126: public string key { get; set; }
127: public PhoneType PhoneTypeValue { get; set; }
128: }
129: #endregion
130:
131: #region PhoneType 号码类别
132: /// <summary>
133: /// 号码类别
134: /// </summary>
135: public enum PhoneType
136: {
137: /// <summary>
138: /// 不是一个手机号码
139: /// </summary>
140: NotPhoneNo,
141: /// <summary>
142: /// 未知所属,其他
143: /// </summary>
144: Other,
145: /// <summary>
146: /// 移动号码
147: /// </summary>
148: Mobile,
149: /// <summary>
150: /// 联通号码
151: /// </summary>
152: Unicom,
153: /// <summary>
154: /// 电信号码
155: /// </summary>
156: Telecom
157: }
158: #endregion
159: }