从电话号码确定国家
I have seen a few question on SO similar to what I require but nothing seems to fit the bill.
I am in the position where I need to deal with a call record and determine the country using the phone number. The number dialed can be any country for example:
44 7899455120 - UK
34 965791845 - Spain
355 788415235 - Albania
Obviously the world would be great if all calling codes were two digits but this is not the case. Currently I have a database full of countries and their relevant codes and in order to match I need to effectively take the first digit of the number ie 4
for the UK example and do a query of the database eg:
SELECT * from countries WHERE code LIKE '4%'
This may give me for example 20 results. So I loop again and do say
SELECT * from countries WHERE code LIKE '44%'
This may give me say one result, now I can determine it is UK. Some codes however like Albania are three digits and require more loops and database calls. This seems quite rudimentary and inefficient but as is I cannot think of another way to achieve this. I realise three calls to a database may not seem like much but if you have 1000 calls to deal with they soon add up.
Looking at the following question:
What regular expression will match valid international phone numbers?
There seems to be some great information on validating a number against country codes, but not so much on determining the country code from a number. Any advice or suggestions on a cleaner method would be much appreciated.
Spaces in the phone are shown for clarity
我在SO上看到了一些与我的要求类似的问题,但似乎没有什么比这更合适了。 p >
我处于需要处理通话记录并使用电话号码确定国家/地区的位置。 拨打的号码可以是任何国家/地区,例如: p>
44 7899455120 - UK p>
34 965791845 - 西班牙 p>
355 788415235 - 阿尔巴尼亚 p>
如果所有的呼叫代码都是两位数,显然世界会很棒但事实并非如此。 目前我有一个完整的国家及其相关代码的数据库,为了匹配我需要有效地获取数字的第一个数字,即英国示例的 这可能会给我举例20个结果 。 所以我再次循环,然后说来 p>
这 可能会给我说一个结果,现在我可以确定它是英国。 但有些代码如阿尔巴尼亚是三位数,需要更多的循环和数据库调用。 这似乎相当简陋和低效,但我想不出另一种实现这一目标的方法。 我意识到对数据库的三次调用可能看起来不多,但如果你有1000个电话要处理它们很快就会加起来。 p>
看下面的问题: p>
\ n 似乎有一些关于根据国家/地区代码验证号码的重要信息,但不是根据数字确定国家/地区代码。 任何关于更清洁方法的建议或建议都将不胜感激。 p>
为清晰起见,手机中的空格显示 p>
div> 4 code>,并对数据库进行查询,例如: p>
来自国家的SELECT * WHERE代码LIKE'4%'
code> pre>
SELECT *来自国家WHERE代码LIKE '44%'
code> pre>
A library exists that will parse a string of digits and reformat it to international standards (a number like 4402081231234 to '+44 20 8123 1234'). It will also return the Phone Number region, 'GB' or 'US' from a number, if there is the country code embedded in the number.
https://github.com/googlei18n/libphonenumber The original library is in Java, but there are also versions in Javascript, Python, Ruby and PHP, among others.
Assuming the phone will always look like that:
$phone = "355 788415235"; // Albania
$parts = explode(" ", $phone);
$code = $parts[0]; // First part separated by space, 355.
Then query by that directly. No regular expression needed.
If that's not the case, consider separating the country code from the number on the input level.
On your system, every phone number has white space after country code so you can use it to determine country.
-
Create a table which has all country codes. Lıke
id | country | code
1 | Turkey | 90
2 | Spain | 34
(There is a table for you: http://erikastokes.com/mysql-help/country.sql.txt )
Than explode your phone number. Delimeter is white space " ".
$phoneNumber = "355 788415235";
$countryCode = explode(" ",$phoneNumber); // it divides phone number to two parts.
$countryCode = $countryCode[0]; // it returns 355. We write index 0 because country code is first part.
//Now you can call your country by country code.
$sqlQuery ="SELECT country FROM yourTableName WHERE code = '$countryCode' ";
...
//it will works like a charm. Because i currently using this.
There is no overlap ambiguity in the country codes. Meaning: the country code 11 is illegal because 1 is assigned to North America. Similarly, 20 is Egypt and there are no other country codes that start with 20. And the country codes that start with 21 are all 3 digits.
Since the is no overlap ambiguity, you can directly search for the country code in one query for the phone number 12125551212 like this:
select country
, code
from countrycodes
where code in ('121', '12', '1')
Again, there are no country codes 121 or 12, so the only criteria that will match is the 1.