手機號段確定手機號碼服務商類別

來源:互聯網
上載者:User

標籤:style   class   code   tar   ext   color   

   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 檢測號碼的Regex
  17:          /// <summary>
  18:          /// 檢測號碼的Regex
  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:  }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.