function funcMtel_v1(string $str): bool {
return (bool)preg_match('/^(?:13\d|15[03689])\d{8}$/', $str);
}
// 測試 18678785887 → false ❌ (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);
}
function funcMtel_v3(string $str): bool {
return (bool)preg_match('/^(?:1[3458]\d|15[03689])\d{8}$/', $str);
}
// 測試 18678785887 → true ✅
// 但缺少 16/17/19 段
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);
}
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']
config/mobile_prefixes.php),定期對照工信部公告更新,無需改代碼。/api/validation/mobile-prefixes 供前端動態獲取。手機號碼正則不是「寫一次就好」的靜態代碼,而是需要持續維護的業務規則。採用 白名單陣列配置化 + 版本化發布 + 前後端同步 + OTP 實名驗證 的組合策略,才能在號段頻繁擴展的環境下,保持驗證準確、維護低成本、用戶體驗流暢。