+ if (str_starts_with($phone, '00')) { $phone = '+' . substr($phone, 2); } elseif ($phone[0] !== '+') { // 没有 +,默认补一个(大多数线索都是国际号) $phone = '+' . $phone; } $digits = ltrim($phone, '+'); if ($digits === '') { return null; } // 3. 国家区号最长 3 位,从长到短匹配 for ($len = 3; $len >= 1; $len--) { $code = substr($digits, 0, $len); if ($code === '') { continue; } $country = Db::table('country') ->where('phone_code', $code) ->first(); if ($country) { return (int)$country->id; } } // 没找到 return null; } }