处理电话号码格式

发布于 2024-12-09 02:59:26 字数 1073 浏览 0 评论 0原文

我想我在这里面临着一个悖论。

我想做的是,当我接听/拨打电话时,我有号码,所以我需要知道它是否是国际号码,是否是本地号码等。

问题是:

  1. 对我来说,要知道它是否是数字是国际性的,我需要解析它并检查它的长度,但是,不同国家的长度不同,所以,我应该做一个解析和识别每个国家的方法吗? (我认为不起作用);
  2. 为了知道它是否是本地号码,我需要区号,所以我必须做同样的事情,解析号码并检查长度,根据区号长度获取第一个号码;

找到这个问题的解决方案有点困难。库 libphonenumber 提供了很多有用的类,但我认为可以帮助我的那个,把我带到了另一个悖论。

方法phoneUtil.parse(number,countryAcronym)返回带有国家/地区代码的号码,但它的作用是,如果我传递带有首字母缩略词“US”的号码,它会返回带有国家/地区代码“1”的号码,现在如果我更改“BR”的缩写会更改号码并返回“55”,这是巴西的国家/地区代码。所以,无论如何,我需要根据我得到的号码提供国家/地区缩写。

例如:

numberReturned = phoneUtil.parse(phoneNumber, "US");
phoneUtil.format(numberReturned, PhoneNumberFormat.INTERNATIONAL);

上面的代码返回带有美国国家/地区代码的号码,但现在如果我将“US”更改为任何其他国家/地区的缩写,它将返回相同的号码,但带有该国家/地区的国家/地区代码。

我知道这个库不应该猜测号码来自哪个国家(那太棒了!!),但这就是我需要的。

这真的让我的头脑发疯。我需要来自 SO 的明智法师的良好建议。

如果您能帮助我做出一个好的决定,我将非常感激。

谢谢。


PS:如果您已经使用 libphonenumber 并且对此有更多经验,请指导我使用哪个类(如果有一个类能够解决此问题)。 =)

I think I'm facing a paradox here.

What I'm trying to do is when I receive/make a call, I have the number, so I need to know if its an international number, if its a local number, etc.

The problem is:

  1. For me to know if a number is international, I need to parse it and check its length, but, the length differs from country to country, so, should I do a method that parses and recognizes for each country? (Unfunctional in my opinion);
  2. For me to know if its a local number, I need the area code, so I have to make the same thing, parse the number and check the lenght, get the first numbers based on the area code lenght;

Its kinda hard to find the solution for this. The library libphonenumber offers a lot of usefull classes, but the one that I thought that could help me, took me to another paradox.

The method phoneUtil.parse(number, countryAcronym) returns the number with its country code, but what it does is, if I pass the number with the acronym "US" it return the number with country code '1', now if I change the acronym to "BR" it changes the number and return '55' that is the country code for Brazil. So, anyways, I need the country acronym based on the number I get.

EX:

numberReturned = phoneUtil.parse(phoneNumber, "US");
phoneUtil.format(numberReturned, PhoneNumberFormat.INTERNATIONAL);

The above code, returns the number with the US country code but now if I change the "US" to any other country acronym it will return the same number but with the country code of that country.

I know that this lib is not supposed to guess from which country the number is (THAT WOULD BE AWESOME!!), but thats what I need.

This is really making my mind goes crazy. I need good advices from the wise mages of SO.

If you please could help me with a good decision, I'd be so thankfull.

Thanks.


PS: If you already use libphonenumber and has more experience with this, please guide me on which class to use, if there is one capable of solving this problem. =)

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

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

发布评论

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

评论(1

盛夏已如深秋| 2024-12-16 02:59:26

1) phoneUtil.parse 的第二个参数必须与您当前所在的国家/地区匹配 - 如果收到的电话号码不包含国际前缀,则使用该参数。这就是为什么当您更改参数时会得到不同的结果:您传递给它的电话号码不包含这样的前缀,因此它只使用您告诉它的内容。

任何用于确定电话号码是否为国际电话号码的解析解决方案都需要注意这一点:根据来源,即使是国内号码也可能用国际拨号前缀表示(通常抽象为 +,因为它在国家/地区之间有所不同) ,但这并不能保证)。

2)对于区号解析,没有通用标准;有些国家不使用它们,即使在一个国家内,区号也可能有不同的长度(例如德国)。我不知道有这样的国际图书馆 - 并且快速搜索没有找到任何东西(尽管这并不意味着不存在)。您可能需要在这里自己动手;如果你只需要支持一个国家,这应该不会太难。

1) The second parameter to phoneUtil.parse must match the country you're currently in - it's used if the phone number received does not include an international prefix. That's why you get different results when you change the parameter: the phone number you pass it does not contain such a prefix, so it just uses what you've told it.

Any parsing solution set to determine if the phone number is international or not will need to be aware of this: depending on the source, even a national number may be represented with the international dialing prefix (usually abstracted as +, since it differs between countries, but this is not guaranteed).

2) For area code parsing, there is no universal standard; some countries don't use them, and even within a country, area codes may have differing lengths (e.g. Germany). I'm not aware of an international library for this - and a quick search doesn't find anything (though that doesn't mean one does not exist). You might need to roll your own here; if you only need to support a single country, this shouldn't be too hard.

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