當前號段:11 位數字,以 1 開頭,第二位 3-9。主要運營商號段:
<?php
function isValidMobileCN(string $mobile): bool
{
// 1[3-9]\d{9} 涵蓋所有 11 位、以 1 開頭、第二位 3-9 的號碼
// 若需嚴格限定已知段,可用下方嚴格版
return (bool) preg_match('/^1[3-9]\d{9}$/', $mobile);
}
// 測試
$tests = [
'13800138000' => true,
'15912345678' => true,
'19212345678' => true, // 廣電
'17012345678' => true, // 虛擬
'12800138000' => false, // 第二位不合法
'1380013800' => false, // 10 位
'138001380000' => false, // 12 位
'03800138000' => false, // 不以 1 開頭
];
foreach ($tests as $m => $exp) {
$r = isValidMobileCN($m);
echo "$m => " . ($r ? '✅' : '❌') . " (預期: " . ($exp ? '✅' : '❌') . ")" . PHP_EOL;
}
?>
<?php
function isValidMobileCNStrict(string $mobile): bool
{
// 依工信部公佈號段整理(2024),可定期更新
$pattern = '/^1(?:3\d|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8\d|9[0-9])\d{8}$/';
return (bool) preg_match($pattern, $mobile);
}
?>
<?php
function getMobileCarrier(string $mobile): ?string
{
if (!isValidMobileCN($mobile)) return null;
$prefix3 = substr($mobile, 0, 3);
$prefix4 = substr($mobile, 0, 4);
$carriers = [
'mobile' => [
'134','135','136','137','138','139',
'147','148',
'150','151','152','157','158','159',
'172','178',
'182','183','184','187','188',
'195','197','198',
],
'unicom' => [
'130','131','132',
'145','146',
'155','156',
'166','167',
'171','175','176',
'185','186',
'196',
],
'telecom' => [
'133',
'149',
'153',
'173','174','177',
'180','181','189',
'190','191','193','199',
],
'broadcast' => ['192'],
];
foreach ($carriers as $carrier => $prefixes) {
if (in_array($prefix3, $prefixes, true) || in_array($prefix4, $prefixes, true)) {
return $carrier;
}
}
return 'unknown';
}
// 測試
echo getMobileCarrier('13800138000'); // mobile
echo getMobileCarrier('18612345678'); // unicom
echo getMobileCarrier('18912345678'); // telecom
?>
<input type="tel" name="mobile" pattern="1[3-9]\d{9}"
title="請輸入 11 位中國大陸手機號碼"
placeholder="13800138000" required>
| 錯誤正則 | 問題 | 修正 |
|---|---|---|
/^13\d{9}$/ | 僅支援 13 開頭,漏掉 14-19 段 | 用 /^1[3-9]\d{9}$/ 或嚴格版 |
/^1[34578]\d{9}$/ | 漏掉 16、19 段 | 同上 |
/^\d{11}$/ | 允許 0/2 開頭,非手機號 | 限定首碼 1、次碼 3-9 |
通用驗證用 /^1[3-9]\d{9}$/ 簡潔且覆蓋率高;嚴格業務用號段白名單正則並定期更新。切記:格式驗證 ≠ 號碼真實有效,關鍵業務需發送簡訊驗證碼(SMS OTP)確認擁有權。