有没有电话号码清理库之类的东西

发布于 2024-11-17 20:23:15 字数 536 浏览 5 评论 0原文

我想找到一个工具/库,可以获取用户通过网站输入的自由格式电话号码,并将其解析为适合从给定国家/地区拨打的号码。

这并不像乍听起来那么简单。该网站允许世界各地的人们以任何他们选择的方式输入他们的号码,因此很多人输入国家号码(另一个字段中也提供了国家/地区)。有些人以“正确”的格式(使用“+”)输入国际号码,有些人使用其所在国家/地区的国际前缀输入的国际号码的正确性稍差。

我想告诉图书馆/工具我拨打的国家/地区,以及自由格式的电话号码以及可选的对应国家/地区(因为如果未输入,这将有助于生成国际代码),并且它使用最佳猜测在我的呼叫国家/地区有效的号码的已知模式

因此,例如,从美国拨打英国号码时:

+44 (0) 1225-344567 => 011441225344567

或者从英国拨打美国号码时:

(613) 4562342 => 0016134562342

任何人都知道(最好是 .NET Framework) -友好的)解决方案无疑会避开我重新发明轮子

I would like to find a tool/library that can take user-entered free format telephone numbers entered through a web site and parse them into a number suitable for calling from a given country.

This is not quite as simple as it may at first sound. The website allows people all around the world to enter their number in any way they choose, so many people enter national numbers (a country is also provided in another field). Some people enter the international number in "correct" formats (with the "+"), some people enter it slightly less correctly, using their country's international prefix.

I would like to tell the library/tool the country that I'm dialling from, and the free-format telephone number and optionally the country that this corresponds to (as this will help generate the international code if not entered), and it use known patterns to best-guess the number that will work in my calling country

So, for example, when calling from the US to a UK number:

+44 (0) 1225-344567 => 011441225344567

Or when calling from the UK to a US number:

(613) 4562342 => 0016134562342

Anyone know of any (ideally .NET Framework-friendly) solutions that will avoid me undoubtedly re-inventing the wheel

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

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

发布评论

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

评论(5

转身泪倾城 2024-11-24 20:23:15

我已经实现了这个,马克。我在一家无线运营商工作,拥有一个国际短信发送应用程序。我不知道有任何第三方库实施这些规则。如上所述,由于电话号码格式因国家/地区而异,因此不能仅处理随机输入。德国等一些国家/地区有可变长度的区号和电话号码。如果他们不把国家代码放在那里,你就沉没了。但是,就我而言,如果缺少国家/地区代码,我可以假设它是美国电话号码。事实证明,我的过滤器结果对于我的用户和输入非常准确。

人们可以做出一些假设,通过了解目标受众、记录输入和分析,人们可以拨通事情。我的第一个实现是一个“白标签”网络应用程序,用于由世界各地的不同人进行测试。我很快发现,大多数外国人都是齐心协力,习惯了准标准+格式。通常美国人在输入电话号码时最容易混淆。欧洲人非常习惯国际拨号。

第一条规则是删除除数字和前导“+”之外的所有内容。

如果结果数字少于 8 位,则为垃圾数字,请提供用户错误。

如果结果数字以“+”开头,则假定它是标准格式,并且接下来的 2-6 位数字代表“国家/地区代码”。找出国家/地区代码,然后根据该国家/地区的规则处理剩余的数字。

如果号码以 0 开头,假设有人输入国际访问前缀,并去掉前导 0 和 1,则:
如果剩余号码为10位,则认为是美国人输入的美国号码,进行相应处理。如果剩余号码不是10位,而是至少8位,则假设前2-6位是国家代码,查找国家代码,并根据该国家的规则进行处理。

如果号码以 1 开头,并且共有 11 位数字,则假设它是美国(或加勒比岛)号码,并进行相应处理。

如果号码以 1 开头且总数不是 11 位,则去掉前导 1,查看是否还剩下至少 8 位数字,并假设剩余的前 2 到 6 位数字是国家/地区代码,并根据国家/地区规则进行处理。

最后,对于不以+、0或1开头的数字,并且至少有8位数字,假设它是没有+的标准表示法,即国家代码在前,则使用前2到6位数字作为国家代码并根据该国的规则进行处理。

所有这一切的诀窍是获得世界上所有国家/地区代码的映射以及每个国家/地区的编号计划信息。我有那张地图以及许多国家的规则。如果您想要这些信息,我很乐意与您分享,以及一些用于确定哪个国家/地区的 C# 代码。给我留言。

对此的一个巨大帮助是将您的软件猜测的国家/地区名称回发给用户。如果他们尝试输入德国电话号码,并且您的软件询问他们是否尝试拨打关岛,他们会很快明白!

I've implemented just this, Mark. I work for a wireless carrier and have an international SMS sending application. I am not aware of any third party libraries that implement these rules. As mentioned above, one cannot just deal with random input as the phone number formats vary among countries. Some countries such as Germany have variable length area codes and phone numbers. If they don't put the country code in there you are sunk. However, in my case I can assume if it's missing a country code it's a USA phone number. The results of my filter have proven to be very accurate with the users and input I've had.

One can make some assumptions, and by knowing the target audience, logging the inputs, and analyzing, one can get things dialed in. My first implementation was for a "white label" web app that is used for testing by various people around the world. I rapidly found that most foreign people have their act together and are used to the quasi-standard + format. It's usually Americans entering phone numbers that are most confused. Europeans are very used to international dialing.

First rule is to strip out everything but digits and a leading '+'.

If the resulting number has fewer than 8 digits, it's junk, provide user error.

If the resulting number starts with a '+' assume it is the standard format and that the next 2-6 digits represent the "country code". Figure out the country code then process the remaining digits according to the rules for that country.

If the number starts with 0, assume someone put in an international access prefix, and strip off leading 0's and 1's, then:
if the remaining number is 10 digits, assume it was a USA number entered by an American, and handle accordingly. If the remaining number is not 10 digits, but at least 8, assume the first 2-6 digits are a country code, lookup the country code, and process according to that country's rules.

If the number starts with a 1, and it is 11 digits total, assume it is a USA (or Caribbean Island) number, and process accordingly.

IF the number starts with a 1 and is not 11 digits total, strip the leading 1s, see if there are at least 8 digits left, and assume the remaining leading 2 to 6 digits are a country code and process per the country rules.

Finally, with the number not leading with +, 0, or 1, and being at least 8 digits, assume it is in the standard notation without the +, that is country code first, use the first 2 to 6 digits as the country code and process according to that country's rules.

The trick in all of this is to have a mapping of all the world's country codes, and the numbering plan information for each country. I have that map, and the rules for many countries. If you would like this information I'd be happy to share, along with some C# code that figures out which country. Message me.

A huge help in this is to post back the name of the country your software is guessing to the user. They will understand rapidly if they are trying to enter a German phone number and your software asks them if they are trying to dial Guam!

别挽留 2024-11-24 20:23:15

是的:Google 用于解析、格式化、存储和验证国际电话号码的通用 Java、C++ 和 Javascript 库现已移植到 C#:

https://github.com/erezak/libphonenumber-csharp

祝你好运!

Yes: Google's common Java, C++ and Javascript library for parsing, formatting, storing and validating international phone numbers has now been ported to C#:

https://github.com/erezak/libphonenumber-csharp

Good Luck!

雨后彩虹 2024-11-24 20:23:15

Google 用于解析、格式化、存储和验证国际电话号码的库现已移植到 C#。

https://github.com/erezak/libphonenumber-csharp

Google's library library for parsing, formatting, storing and validating international phone numbers has now been ported to C#.

https://github.com/erezak/libphonenumber-csharp

北方的巷 2024-11-24 20:23:15

不,没有:) 管理国家之间电话号码的规则很少。有些国家甚至有多个代表处。

例如,英国的区号大小可变,例如:

  • 0121 = 伯明翰
  • 01223 = 剑桥

No, there isn't :) There are very few rules that govern telephone numbers between nations. Some countries even have multiple representations.

The UK for instance has variable sized area codes eg:

  • 0121 = Birmingham
  • 01223 = Cambridge
德意的啸 2024-11-24 20:23:15

国际(和国内)电话号码的格式有一个“标准”。它们看起来像这样:+31 42 123 4567(+国家/地区前缀号码呼叫)。示例取自维基百科。尽管您不应该期望任何人以这种方式输入它。根据我的经验,用户很可能会坚持他们习惯的格式。

我不知道有哪个库可以将所有(主要是本地)表示形式转换为我提到的表示形式或任何其他格式。

There is a "standard" to format international ( and national ) phone numbers. They look like this: +31 42 123 4567 (+country prefix number callthrough). Example taken from Wikipedia. Though you shouldn't expect anyone to enter it that way. In my experience users most likely will stick to the format they're used to.

I do not know of any library that will convert between all ( mostly local ) representations to that mentioned by me or any other format.

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