C++ libphonenumber:强大的电话号码解析与格式化库
项目概述
libphonenumber 是 Google 开发的一个开源库,最初用于 Android 系统,现在提供了 C++ 版本。这个库专门用于解析、格式化和验证国际电话号码。它基于 Google 维护的全球电话号码数据库,支持全球所有国家和地区的电话号码格式。
核心功能特性
1. 电话号码解析
- 从各种格式的字符串中提取电话号码
- 自动识别国家/地区代码
- 支持本地和国际格式
2. 电话号码验证
- 验证电话号码的有效性
- 检查号码类型(固定电话、手机、免费电话等)
- 支持严格和宽松两种验证模式
3. 格式标准化
- 将电话号码转换为标准格式
- 支持 E.164 国际格式
- 提供本地格式化选项
4. 地理信息查询
- 根据号码获取地理位置信息
- 识别时区和运营商信息
安装与配置
通过 vcpkg 安装(推荐)
text
vcpkg install libphonenumber
手动编译安装
text
git clone https://github.com/google/libphonenumber.git cd libphonenumber/cpp mkdir build && cd build cmake .. make sudo make install
基础使用示例
示例 1:基本解析与格式化
text
#include <phonenumbers/phonenumberutil.h>
#include <phonenumbers/phonenumber.pb.h>
#include <iostream>
using namespace i18n::phonenumbers;
int main() {
PhoneNumberUtil* phone_util = PhoneNumberUtil::GetInstance();
// 解析电话号码
PhoneNumber number;
std::string formatted_number = "+14155552671";
phone_util->Parse(formatted_number, "US", &number);
// 格式化为国际格式
std::string international_format;
phone_util->Format(number, PhoneNumberUtil::INTERNATIONAL, &international_format);
std::cout << "国际格式: " << international_format << std::endl;
// 格式化为本地格式
std::string national_format;
phone_util->Format(number, PhoneNumberUtil::NATIONAL, &national_format);
std::cout << "本地格式: " << national_format << std::endl;
return 0;
}
示例 2:电话号码验证
text
#include <phonenumbers/phonenumberutil.h>
#include <phonenumbers/phonenumber.pb.h>
#include <iostream>
bool validatePhoneNumber(const std::string& phone_number, const std::string& region_code) {
PhoneNumberUtil* phone_util = PhoneNumberUtil::GetInstance();
PhoneNumber number;
// 解析电话号码
auto result = phone_util->Parse(phone_number, region_code, &number);
if (result != PhoneNumberUtil::NO_PARSING_ERROR) {
std::cout << "解析失败" << std::endl;
return false;
}
// 验证有效性
bool is_valid = phone_util->IsValidNumber(number);
if (is_valid) {
// 获取号码类型
PhoneNumberUtil::PhoneNumberType type = phone_util->GetNumberType(number);
std::string type_str;
switch(type) {
case PhoneNumberUtil::MOBILE:
type_str = "手机号码";
break;
case PhoneNumberUtil::FIXED_LINE:
type_str = "固定电话";
break;
case PhoneNumberUtil::TOLL_FREE:
type_str = "免费电话";
break;
default:
type_str = "其他类型";
}
std::cout << "有效号码 - 类型: " << type_str << std::endl;
} else {
std::cout << "无效号码" << std::endl;
}
return is_valid;
}
int main() {
// 测试不同国家的电话号码
validatePhoneNumber("+8613812345678", "CN"); // 中国手机
validatePhoneNumber("+1-650-253-0000", "US"); // 美国号码
validatePhoneNumber("020 7946 0958", "GB"); // 英国号码
return 0;
}
示例 3:批量处理电话号码
text
#include <phonenumbers/phonenumberutil.h>
#include <phonenumbers/phonenumber.pb.h>
#include <vector>
#include <iostream>
class PhoneNumberProcessor {
private:
PhoneNumberUtil* phone_util_;
public:
PhoneNumberProcessor() {
phone_util_ = PhoneNumberUtil::GetInstance();
}
std::string normalizeNumber(const std::string& raw_number,
const std::string& default_region) {
PhoneNumber number;
std::string normalized;
if (phone_util_->Parse(raw_number, default_region, &number)
== PhoneNumberUtil::NO_PARSING_ERROR) {
phone_util_->Format(number, PhoneNumberUtil::E164, &normalized);
}
return normalized;
}
std::vector<std::string> processPhoneList(
const std::vector<std::pair<std::string, std::string>>& phone_list) {
std::vector<std::string> results;
for (const auto& item : phone_list) {
std::string normalized = normalizeNumber(item.first, item.second);
if (!normalized.empty()) {
results.push_back(normalized);
}
}
return results;
}
};
int main() {
PhoneNumberProcessor processor;
std::vector<std::pair<std::string, std::string>> test_numbers = {
{"(650) 253-0000", "US"},
{"+44 20 7946 0958", "GB"},
{"138-1234-5678", "CN"},
{"invalid number", "US"}
};
auto processed = processor.processPhoneList(test_numbers);
std::cout << "标准化后的电话号码:" << std::endl;
for (const auto& number : processed) {
std::cout << number << std::endl;
}
return 0;
}
示例 4:获取地理位置信息
text
#include <phonenumbers/phonenumberutil.h>
#include <phonenumbers/geocoding/phonenumber_offline_geocoder.h>
#include <phonenumbers/phonenumber.pb.h>
#include <iostream>
void getGeoInfo(const std::string& phone_number, const std::string& language_code) {
PhoneNumberUtil* phone_util = PhoneNumberUtil::GetInstance();
PhoneNumber number;
if (phone_util->Parse(phone_number, "", &number)
== PhoneNumberUtil::NO_PARSING_ERROR) {
// 创建地理编码器
PhoneNumberOfflineGeocoder geocoder;
// 获取地理位置描述
std::string description = geocoder.GetDescriptionForNumber(
number, icu::Locale(language_code.c_str()));
std::cout << "电话号码: " << phone_number << std::endl;
std::cout << "地理位置: " << description << std::endl;
// 获取国家代码
std::string region_code;
phone_util->GetRegionCodeForNumber(number, ®ion_code);
std::cout << "国家代码: " << region_code << std::endl;
}
}
int main() {
// 获取不同语言的地理信息
getGeoInfo("+14155552671", "en"); // 英语
getGeoInfo("+8613812345678", "zh"); // 中文
getGeoInfo("+442079460958", "fr"); // 法语
return 0;
}
高级功能
自定义元数据
text
// 可以加载自定义的电话号码元数据 // 用于支持特殊格式或新地区的号码
异步处理
text
// libphonenumber 支持多线程环境 // 可以结合异步编程模型处理大量电话号码
性能优化建议
- 重用 PhoneNumberUtil 实例:使用
GetInstance()获取单例实例 - 批量处理:对大量号码进行批量处理减少开销
- 缓存结果:对频繁查询的号码进行缓存
- 选择合适的验证级别:根据需求选择严格或宽松验证
应用场景
- 通讯录应用:标准化和验证用户输入的电话号码
- 电商平台:验证用户注册的手机号码
- 客服系统:自动识别来电地区
- 数据分析:清洗和标准化电话号码数据
- 国际化应用:支持多国家电话号码格式
注意事项
- 电话号码数据库需要定期更新以反映最新的号码分配
- 某些功能(如地理位置查询)可能需要额外的数据文件
- 验证规则可能因国家/地区而异
- 需要考虑隐私法规,谨慎处理电话号码数据
总结
libphonenumber 是一个功能全面、可靠性高的电话号码处理库。它的 C++ 版本保持了与 Java 版本相同的功能特性,同时提供了良好的 C++ 接口。无论是简单的号码验证还是复杂的国际化电话系统,libphonenumber 都能提供强大的支持。
通过合理使用这个库,开发者可以确保应用程序能够正确处理全球范围内的电话号码,提升用户体验和数据质量。
libphonenumber_20260205064253.zip
类型:压缩文件|已下载:0|下载方式:免费下载
立即下载




还没有评论,来说两句吧...