电话号码规范化:是否有预先存在的库?

发布于 2024-07-08 11:21:23 字数 348 浏览 8 评论 0 原文

我有一个使用电话号码作为唯一标识符的系统。 因此,我想使用规范化格式对所有电话号码进行格式化。 因为我无法控制源数据,所以我需要自己解析这些数字并格式化它们,然后再将它们添加到我的数据库中。

我即将编写一个解析器,可以读取电话号码并输出标准化的电话格式,但在我这样做之前,我想知道是否有人知道我可以用来格式化电话号码的任何预先存在的库。

如果没有预先存在的库,那么在创建这个可能不明显的功能时我应该记住哪些事情?

虽然我的系统目前仅处理美国号码,但我计划尝试包括对国际号码的支持,以防万一,因为有可能需要它。

编辑 我忘了提及我正在使用 C#.NET 2.0。

I have a system which is using phone numbers as unique identifiers. For this reason, I want to format all phone numbers as they come in using a normalized format. Because I have no control over my source data, I need to parse out these numbers myself and format them before adding them to my DB.

I'm about to write a parser that can read phone numbers in and output a normalized phone format, but before I do I was wondering if anyone knew of any pre-existing libraries I could use to format phone numbers.

If there are no pre-existing libraries out there, what things should I be keeping in mind when creating this feature that may not be obvious?

Although my system is only dealing with US numbers right now, I plan to try to include support for international numbers just in case since there is a chance it will be needed.

Edit I forgot to mention I'm using C#.NET 2.0.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

痴情 2024-07-15 11:21:23

您可以使用 Google 的 libphonenumber。 这是一篇博客文章:

http:// blog.appharbor.com/2012/02/03/net-phone-number-validation-with-google-libphonenumber

解析数字就像安装 NuGet 包,然后执行以下操作:

var util = PhoneNumberUtil.GetInstance();
var number = util.Parse("555-555-5555", "US");

然后您可以按如下所示格式化号码:

util.Format(number, PhoneNumberFormat.E164);

libphonenumber 支持 E.164 以外的多种格式。

You could use libphonenumber from Google. Here's a blog post:

http://blog.appharbor.com/2012/02/03/net-phone-number-validation-with-google-libphonenumber

Parsing numbers is as easy as installing the NuGet package and then doing this:

var util = PhoneNumberUtil.GetInstance();
var number = util.Parse("555-555-5555", "US");

You can then format the number like this:

util.Format(number, PhoneNumberFormat.E164);

libphonenumber supports several formats other than E.164.

半葬歌 2024-07-15 11:21:23

我目前正在参与 OpenMoko 项目,该项目正在开发一款完全开源的手机(包括硬件)。 电话号码标准化存在很多麻烦。 不知道现在有没有人想出好的解决办法。 最大的问题似乎是美国电话号码,因为有时它们的前面带有 1,有时则没有。 根据您在联系人列表中存储的内容,它可能会或可能不会正确显示来电显示信息。 我建议去掉电话号码上的 1(尽管我预计大多数人一开始就不会输入它)。 您可能还需要查找国际号码前面的加号或国家/地区代码。

您可以检查 OpenMoko 网站、邮件列表和源代码管理,看看他们是否已经解决了这个错误。

I'm currently involved in the OpenMoko project, which is developing a completely open source cell phone (including hardware). There has been a lot of trouble around normalizing phone numbers. I don't know if anyone has come up with a good solution yet. The biggest problem seems to be with US phone numbers, since sometimes they come in with a 1 on the front and sometimes not. Depending on what you have stored in your contacts list, it may or may not display the caller ID info correctly. I'd recommend stripping off the 1 on the phone number (though I'd expect most people wouldn't enter it in the first place). You may also need to look for a plus sign or country code on the front of international numbers.

You can check around the OpenMoko website, mailing list, and source control to see if they've solved this bug yet.

世界如花海般美丽 2024-07-15 11:21:23

只需删除任何非数字,可能使用正则表达式:[^\d]

唯一的例外可能是如果您想处理分机号,以区分没有区号但带有 3 位数字的号码分机,或者如果您需要处理国际号码。

Just strip out any non-digits, possibly using a RegEx: [^\d]

The only exception might be if you want to handle extensions, to distinguish a number without an area code but with a 3 digit extension, or if you need to handle international numbers.

请止步禁区 2024-07-15 11:21:23

您需要的是所有国家/地区代码的列表,并开始将字符串的前几个字符与国家/地区代码列表进行匹配,以确保它是正确的,然后对于其余的数字,确保它是所有数字且长度适当,通常从 5 到 5 不等。 10 位数字。

要实现对国家/地区代码的检查,请安装使用网站 NGeoNames nuget .geonames.org" rel="nofollow">www.geonames.org 获取用于匹配的所有国家/地区代码的列表。

What you need is list of all country codes and start matching your string first few characters against list of country codes to make sure it's correct then for the rest of the number, make sure it's all digits and of proper length which usually varies from 5-10 digits.

To achieve checking against country codes, install NGeoNames nuget which uses website www.geonames.org to get list of all country codes to use to match against them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文