iPhone的电话号码分割算法?

发布于 2024-12-20 21:22:06 字数 371 浏览 1 评论 0原文

iPhone有相当不错的电话号码分割功能,例如:

新加坡手机:+65 9852 4135
新加坡居民专线:+65 6325 6524
中国移动:+86 135-6952-3685
中国居民专线:+86 10-65236528
香港:+886 956-238-82
美国:+1 (732) 865-3286

请注意此处的出色功能:
- 国家代码、区号的拆分,其余代码自动拆分;
- 分隔符也很好地适用于不同的国家,例如“()”、“-”和空格。

注意解析逻辑对我来说是可行的,但是,我不知道从哪里获得大多数国家的电话号码格式的知识。
我在哪里可以找到这样的知识,或者实现它的开源代码?

iPhone has a pretty good telephone number splitting function, for example:

Singapore mobile: +65 9852 4135
Singapore resident line: +65 6325 6524
China mobile: +86 135-6952-3685
China resident line: +86 10-65236528
HongKong: +886 956-238-82
USA: +1 (732) 865-3286

Notice the nice features here:
- the splitting of country code, area code, and the rest is automatic;
- the delimiter is also nicely adopted to different countries, e.g. "()", "-" and space.

Note the parsing logic is doable to me, however, I don't know where to get the knowledge of most countries' telephone number format.
where could i found such knowledge, or an open source code that implemented it?

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

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

发布评论

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

评论(3

动听の歌 2024-12-27 21:22:06

您可以使用 libphonenumber 代码库获得类似的功能。

You can get similar functionality with the libphonenumber code library.

上课铃就是安魂曲 2024-12-27 21:22:06

有趣的是,您不能为此使用 NSNumberFormatter,但您可以为其编写自己的自定义类。只需创建一个新类,设置诸如 countryCodeareaCodenumber 等属性,然后创建一个根据 countryCodeareaCodenumber 格式化数字的方法。代码>国家代码。

这是一个很好的例子: http://the- lost-beauty.blogspot.com/2010/01/locale-sensitive-phone-number.html

Interestingly enough, you cannot use an NSNumberFormatter for this, but you can write your own custom class for it. Just create a new class, set properties such as countryCode, areaCode and number, and then create a method that formats the number based on the countryCode.

Here's a great example: http://the-lost-beauty.blogspot.com/2010/01/locale-sensitive-phone-number.html

狠疯拽 2024-12-27 21:22:06

顺便说一句:一位朋友告诉我,他必须维护一个巨大的正则表达式,可以从世界各地数百个国家截获的通信中挑选出电话号码。这非常重要。

值得庆幸的是,您的问题更容易,因为您只需一张包含每个国家/地区格式的表格:

format[usa] = "+d (ddd) ddd-dddd";
format[hk] = "+ddd ddd-ddd-dd";
format[china_mobile] = "+dd ddd-dddd-dddd";
...

然后在打印时,您只需根据需要从每个 d 位置的电话号码字符串中输出一位数字。这假设您知道该国家/地区,这对于电话设备来说是一个足够安全的假设 - 为少数周边国家/地区选择“默认”格式。

由于某些国家/地区具有不同长度的不同格式,因此您可能需要存储您的表格包含附加信息:

format[germany][10] = "..."
format[germany][11] = "....."

As an aside: a friend told me about a gigantic regular expression he had to maintain that could pick telephone numbers out of intercepted communications from hundreds of countries around the world. It was very non-trivial.

Thankfully your problem is easier, as you can just have a table with the per-country formats:

format[usa] = "+d (ddd) ddd-dddd";
format[hk] = "+ddd ddd-ddd-dd";
format[china_mobile] = "+dd ddd-dddd-dddd";
...

Then when you're printing, you simply output one digit from the phone number string in each d spot as needed. This assumes you know the country, which is a safe enough assumption for telephone devices -- pick "default" formats for the few surrounding countries.

Since some countries have different formats with different lengths you might need to store your table with additional information:

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