萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> PHP 程式設計

PHP 手機號碼驗證正則演進:從 13/15 到 13-19 全號段支援

演進歷程

版本 1:僅支援 13/15 開頭 (舊版)

function funcMtel_v1(string $str): bool {
  return (bool)preg_match('/^(?:13\d|15[03689])\d{8}$/', $str);
}
// 測試 18678785887 → false ❌ (18段未涵蓋)

版本 2:嘗試加入 18 段但語法錯誤

function funcMtel_v2(string $str): bool {
  // 錯誤:(?:1[3|4|5|8]\d{1}|15[03689]) 字符類 [3|4|5|8] 匹配單字符 '3' '|' '4' '|' '5' '|' '8'
  // 正確應為 [3458] 或 (?:3|4|5|8)
  return (bool)preg_match('/(?:1[3|4|5|8]\d{1}|15[03689])\d{8}$/', $str);
}

版本 3:修正字符類,支援 13/14/15/18 (不完整)

function funcMtel_v3(string $str): bool {
  return (bool)preg_match('/^(?:1[3458]\d|15[03689])\d{8}$/', $str);
}
// 測試 18678785887 → true ✅
// 但缺少 16/17/19 段

版本 4:2024 最新完整版 (含所有工信部公布號段)

function isMobileCN(string $mobile): bool {
  // 快速長度/格式預檢
  if (!preg_match('/^1[3-9]\d{9}$/u', $mobile)) return false;
  
  // 精確號段白名單 (依工信部最新公告更新)
  $validPrefixes = [
    '13' => [0,1,2,3,4,5,6,7,8,9],
    '14' => [5,6,7,8,9],        // 物聯網、衛星
    '15' => [0,1,2,3,5,6,7,8,9], // 缺 4
    '16' => [2,5,6,7],          // 162/165/166/167
    '17' => [0,1,2,3,4,5,6,7,8], // 170/171/172/173/175/176/177/178
    '18' => [0,1,2,3,4,5,6,7,8,9],
    '19' => [1,3,5,8,9],        // 191/193/195/198/199
  ];
  
  $prefix3 = substr($mobile, 0, 3); // 如 '186'
  $head2 = substr($prefix3, 0, 2);  // '18'
  $third = (int)$prefix3[2];        // 6
  
  return isset($validPrefixes[$head2]) && in_array($third, $validPrefixes[$head2], true);
}

台灣手機號碼驗證

function isMobileTW(string $mobile): bool {
  // 09 開頭,10 位數字
  return (bool)preg_match('/^09\d{8}$/u', $mobile);
}

國際通用驗證 (E.164 格式)

function isMobileInternational(string $mobile): bool {
  // + 國碼(1-3位) 國內號碼(最少6位),總長 8-15 位數字
  // 允許 + 或 00 開頭
  return (bool)preg_match('/^(?:\+|00)[1-9]\d{6,14}$/u', $mobile);
}

最佳實踐:封裝為可維護的驗證類

<?php
class MobileValidator {
  private static array $cnPrefixes = [
    '13' => [0,1,2,3,4,5,6,7,8,9],
    '14' => [5,6,7,8,9],
    '15' => [0,1,2,3,5,6,7,8,9],
    '16' => [2,5,6,7],
    '17' => [0,1,2,3,4,5,6,7,8],
    '18' => [0,1,2,3,4,5,6,7,8,9],
    '19' => [1,3,5,8,9],
  ];
  
  public static function isValidCN(string $mobile): bool {
    if (!preg_match('/^1[3-9]\d{9}$/u', $mobile)) return false;
    $head2 = substr($mobile, 0, 2);
    $third = (int)$mobile[2];
    return isset(self::$cnPrefixes[$head2]) && in_array($third, self::$cnPrefixes[$head2], true);
  }
  
  public static function isValidTW(string $mobile): bool {
    return (bool)preg_match('/^09\d{8}$/u', $mobile);
  }
  
  public static function isValidInternational(string $mobile): bool {
    return (bool)preg_match('/^(?:\+|00)[1-9]\d{6,14}$/u', $mobile);
  }
  
  // 統一入口:自動偵測格式
  public static function validate(string $mobile): array {
    $mobile = trim($mobile);
    if (self::isValidCN($mobile)) return ['valid' => true, 'type' => 'CN', 'normalized' => $mobile];
    if (self::isValidTW($mobile)) return ['valid' => true, 'type' => 'TW', 'normalized' => $mobile];
    if (self::isValidInternational($mobile)) return ['valid' => true, 'type' => 'INTL', 'normalized' => $mobile];
    return ['valid' => false, 'type' => null, 'normalized' => null];
  }
}

// 使用
var_dump(MobileValidator::validate('18678785887')); // ['valid'=>true, 'type'=>'CN', 'normalized'=>'18678785887']
var_dump(MobileValidator::validate('0912345678'));  // ['valid'=>true, 'type'=>'TW', 'normalized'=>'0912345678']
var_dump(MobileValidator::validate('+8618678785887'));// ['valid'=>true, 'type'=>'INTL', 'normalized'=>'+8618678785887']

維護建議

  1. 號段變動頻繁:將白名單陣列獨立為配置檔 (config/mobile_prefixes.php),定期對照工信部公告更新,無需改代碼。
  2. 單元測試覆蓋:每次更新號段後,跑測試確保舊有效號碼仍通過、新號碼通過、無效號碼拒絕。
  3. 前端同步:前端驗證邏輯 (JS 正則) 需與後端白名單同步更新,建議由後端 API 提供 /api/validation/mobile-prefixes 供前端動態獲取。
  4. 不要過度驗證:格式驗證只能檢查結構,**無法確認號碼真實存在/在用/歸屬**,業務關鍵場景 (註冊、綁定、金流) 必須配合 **簡訊驗證碼 (OTP)** 實名驗證。

結語

手機號碼正則不是「寫一次就好」的靜態代碼,而是需要持續維護的業務規則。採用 白名單陣列配置化 + 版本化發布 + 前後端同步 + OTP 實名驗證 的組合策略,才能在號段頻繁擴展的環境下,保持驗證準確、維護低成本、用戶體驗流暢。

copyright © 萬盛學電腦網 all rights reserved