iPhone的电话号码分割算法?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 libphonenumber 代码库获得类似的功能。
You can get similar functionality with the libphonenumber code library.
有趣的是,您不能为此使用 NSNumberFormatter,但您可以为其编写自己的自定义类。只需创建一个新类,设置诸如
countryCode
、areaCode
和number
等属性,然后创建一个根据countryCode
、areaCode
和number
格式化数字的方法。代码>国家代码。这是一个很好的例子: 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 ascountryCode
,areaCode
andnumber
, and then create a method that formats the number based on thecountryCode
.Here's a great example: http://the-lost-beauty.blogspot.com/2010/01/locale-sensitive-phone-number.html
顺便说一句:一位朋友告诉我,他必须维护一个巨大的正则表达式,可以从世界各地数百个国家截获的通信中挑选出电话号码。这非常重要。
值得庆幸的是,您的问题更容易,因为您只需一张包含每个国家/地区格式的表格:
然后在打印时,您只需根据需要从每个
d
位置的电话号码字符串中输出一位数字。这假设您知道该国家/地区,这对于电话设备来说是一个足够安全的假设 - 为少数周边国家/地区选择“默认”格式。由于某些国家/地区具有不同长度的不同格式,因此您可能需要存储您的表格包含附加信息:
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:
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: