简体   繁体   English

格式化电话号码

[英]Format telephone number

I have to format a telephone number list, and I'd wish to extract and separate the prefix from the number for better viewing. 我必须格式化电话号码列表,并且希望从号码中提取前缀并将其分开,以便更好地查看。
I have a list of all possible prefixes, but there is no regular pattern. 我列出了所有可能的前缀,但是没有常规模式。
I mean, I could have these numbers: 我的意思是,我可以有以下数字:

00 - 12345 (short prefix) 00-12345(短前缀)
0000 - 12345 (long prefix) 0000-12345(长前缀)

How can I manage that? 我该如何处理? Numbers are plain, without any special char (ie without / \\ - . , ecc ecc). 数字是简单的,没有任何特殊的字符(即,没有/ \\-。,ecc ecc)。
Prefixes are like that: 前缀是这样的:

030 030
031 031
0321 0321
0322 0322
... ...
... ...

Most of the time I have the town of the customer (it's not required) so, usually i can get the prefix from there, but that's not a sure thing, since town and telephone couldn't be linked. 大多数时候,我都有客户所在的城镇(不是必需的),所以通常我可以从那里得到前缀,但这并不是确定的事情,因为无法链接城镇电话

== EDIT == ==编辑==
Prefix list is 231 entries long. 前缀列表的长度为231个条目。 Maybe I'll add somthing more, so take 300 as safe value 也许我会加些东西,所以取300为安全值
Moreover, prefixes come from a single country only (Italy) 此外,前缀仅来自单个国家/地区(意大利)
I have to save plain numbers without any separator so users can search for it. 我必须保存无任何分隔符的纯数字,以便用户可以搜索它。 Infact if they put separators they will never able to find again that. 实际上,如果他们放置分隔符,他们将再也找不到。

More info 更多信息
Prefix ALWAYS starts with a leading 0 , its lenght ranges from 2-4 前缀始终以0开头,长度范围为2-4

But the more i study this thing, the more i think i can't work it out :( 但是我越研究这个东西,我就越认为我无法解决:(

Because of the extremely varied telephone number formats used around the world , it's probably going to be tough to correctly parse any phone number that is put into your system. 由于世界各地使用的电话号码格式极为不同 ,因此正确解析放入系统中的任何电话号码可能会很困难。

I'm not certain if it would make your ask any easier, but I had the idea that parsing from Right-to-Left might be easier for you, since it's the Prefix length that's unknown 我不确定是否可以使您的提问变得更轻松,但是我有一个想法, 从右到左的解析对您来说可能会更容易,因为前缀长度未知

What a pain. 真痛苦 I would use a logic funnel to narrow possible choices and finally take a best guess. 我将使用逻辑漏斗来缩小可能的选择范围,最后进行最佳猜测。

First, test if the first few numbers can match anything on your prefix list. 首先,测试前几个数字是否可以匹配前缀列表中的任何内容。 For some, hopefully only one prefix can possibly be correct. 对于某些人,希望只有一个前缀可以正确。

Then, perhaps you could use the city to eliminate prefixes from entirely different countries. 然后,也许您可​​以使用城市来消除来自完全不同国家的前缀。

Finally, you could default to the most popular format for prefixes. 最后,您可以默认使用最流行的前缀格式。

Without any other information, you can't do better than a good guess unless you want to default to no format at all. 没有任何其他信息,除非您不希望默认不使用任何格式,否则您总会比猜测更好。

I'm really confused. 我真的很困惑 What do you mean, "extract and separate"? “提取和分离”是什么意思? My guess is these phone numbers are in a MySQL database, and you get to use PHP. 我的猜测是这些电话号码位于MySQL数据库中,您可以使用PHP。 Are you trying to get the prefix from the numbers, and then insert them into a different field in the same row? 您是否要从数字中获取前缀,然后将其插入同一行的其他字段中? Are you pulling these numbers from the database, and you would just like to print the prefixes to the screen? 您是否要从数据库中提取这些数字,并且只想将前缀打印到屏幕上?

Regardless of what you're trying to do, and taking for granted that you're using PHP and regexs, isn't this essentially what you're looking for?: 无论您要做什么,并且理所当然地认为您正在使用PHP和正则表达式,这实际上不是您要找的东西吗?:

$telephone_number = '333-12345';
$matched = array();
preg_match('~^(\d+)-~', $telephone_number, $matched);
$matched[1]  // Should be '333'

ok, I worked it out. 好的,我解决了。
I saw that there aren't shor prefixes that share chars with longer one. 我看到没有与更长的字符共享字符的shor前缀。
I mean: 我的意思是:
02 -> there will never be a longer prefix as 021, 022 and so on 02->永远不会有更长的前缀021、022,依此类推

so things are pretty easy now: 所以现在事情很简单:

I get first 4 numbers -> is that in my prefix table? 我得到前4个数字->是在前缀表中吗?
YES: stop here NO: get first 3 是:在这里停止否:首先获得3

and so on.. 等等..

thanks for your help 谢谢你的帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM