电话号码解析器

用于解析电话号码的 Dart 库。灵感来自 Google 的 libphonenumber 和 iOS 的 PhoneNumberKit。

与libphonenumber相比,此库的优势在于它即时支持所有平台(无需通道)。

特点

  • 在文本中查找电话号码
  • 验证电话号码
  • 查找电话号码的地区
  • 电话号码解析
  • 轻量级解析器,适用于大小受限的应用
  • 支持东亚阿拉伯数字
  • 使用了Google的libPhoneNumber项目的同类最佳元数据。

解析器

有两个解析器:LightPhoneParser和PhoneParser

LightPhoneParser

  • 更小的体积(可以被进一步摇树优化)
  • 主要使用长度信息进行验证
  • 速度快

PhoneParser

  • 更准确
  • 更多实用工具
  • 更大的体积
  • 计算量更大
  • 使用模式匹配

PhoneParser用法

  final parser = PhoneParser();
  // creation
  final frPhone = parser.parseRaw('+33 655 5705 76');
  final frPhone1 = parser.parseWithIsoCode('fr', '655 5705 76');
  final frPhone2 = parser.parseWithDialCode('33', '655 5705 76');
  final frPhone3 = parser.parseWithIsoCode('fr', '0655 5705 76');
  final allSame =
      frPhone == frPhone1 && frPhone == frPhone2 && frPhone == frPhone3;
  print(allSame); // true

  // validation
  print(parser.validate(frPhone1)); // true
  print(parser.validate(frPhone1, PhoneNumberType.mobile)); // true
  print(parser.validate(frPhone1, PhoneNumberType.fixedLine)); // false

  // changing the country
  final esPhone = parser.copyWithIsoCode(frPhone, 'ES');
  print(esPhone.dialCode); // 34
  print(esPhone.isoCode); // ES
  print(esPhone.international); // '+34655570576'

  // utils
  final text = 'hey my phone number is: +33 939 876 218';
  final found = parser.findPotentialPhoneNumbers(text);
  print(text.substring(found.first.start, found.first.end)); 

LightPhoneParser用法

  final parser = LightPhoneParser();
  // creation
  final frPhone = parser.parseWithIsoCode('fr', '+33 655 5705 76');
  final frPhone1 = parser.parseWithIsoCode('fr', '655 5705 76');
  final frPhone2 = parser.parseWithIsoCode('fr', '655 5705 76');
  final frPhone3 = parser.parseWithIsoCode('fr', '0655 5705 76');
  final allSame =
      frPhone == frPhone1 && frPhone == frPhone2 && frPhone == frPhone3;
  print(allSame); // true

  // validation
  print(parser.validate(frPhone1)); // true
  print(parser.validate(frPhone1, PhoneNumberType.mobile)); // true
  print(parser.validate(frPhone1, PhoneNumberType.fixedLine)); // false

  // changing the country
  final esPhone = parser.copyWithIsoCode(frPhone, 'ES');
  print(esPhone.dialCode); // 34
  print(esPhone.isoCode); // ES
  print(esPhone.international); // '+34655570576'

  // utils
  final text = 'hey my phone number is: +33 939 876 218';
  final found = parser.findPotentialPhoneNumbers(text);
  print(text.substring(found.first.start, found.first.end));

 

迁移到1.0.0

1.0.0引入了两个解析器

  • PhoneParser:重量级,更准确,计算量更大(依赖于模式匹配)
  • LightPhoneParser:轻量级,一定程度上准确,计算量较小(依赖于长度)
    如果基于长度的轻量级验证足够,则可以使用LightPhoneParser,前提是您不导入
    另一个解析器,这样可以减小库的体积。

此更改导致了一些不兼容的更改

之前

final frPhone = PhoneNumber.fromRaw('+33 655 5705 76');
final frPhone1 = PhoneNumber.fromIsoCode('FR', '655 5705 76');
final valid = frPhone.validate();

之后

final frPhone = PhoneParser.fromRaw('+33 655 5705 76')
final frPhone1 = LightPhoneParser.fromIsoCode('FR', '655 5705 76');
final valid = PhoneParser.validate(frPhone);

演示

电话号码输入包中有一个演示使用了此解析器:https://cedvdb.github.io/phone_form_field/

GitHub

https://github.com/cedvdb/phone_numbers_parser