Hey guys,
ive got some php which is part of my Call data records for my phone system, and until now I have only had 2 digit country codes to work with which has been quite easy… does anyone know the best way / have hit this issue before of working out weather its a 2 digit or 3 digit country code for the inputted phone number? below is the code I currently use which basically says if the first 2 numbers of the phone number are 00 then get the next 2 as the country code, but as some phone numbers are 2 digit and some are 3 it makes it very difficult
<code>} elseif ($callareacode == '00' AND strlen($qry2['dst']) > 10) {
$intcode = substr(“$qry2[dst]”, 2, 2);
$intquerytype = mysql_query(“SELECT * FROM webtastix_controlpanel.voip_calltypes WHERE code = ‘$intcode’”)
or die("Could not insert data because ".mysql_error());
$intqrytype = mysql_fetch_array( $intquerytype );
Since the phone numbers themselves need to tell the difference you should be able to tell from the first two digits whether you have a two digit country code or just part of a three digit one.
If you look at the list of country codes on http://countrycode.org/ then you can work out which two character values are complete country codes and which are the first two digits of a three digit one. There are also some four digit country codes that all start with 1 where the next three digits is either a USA or Canadian area code or the rest of the country code.
hmmmm I see, so the best way would be to have them all defined and then query the databse on the first 2 numbers, if its not defined then check the first 3 digits and see if that matches? that way logically it should work… assuming of course the first 2 digits never match in a 2 digit country and also part of a 3 digit
How are the numbers being stored? All together in one field, internation dialling code separate?
btw, if you’re not already aware the old mysql_* has been depreceated in version 5.5 of PHP and is being removed from version 7 of PHP (the next version after 5.5), you should be migrating over to either the mysqli_* extension or PDO. Yoo should be using prepared statements
they come straight in from our voip system, so just all together…
knowing that if it doesn’t match a 2 digit country code then it must match a 3 helps though, I was only worried if it was possible it could do both but by the sounds of what fegall said it cant…
yep I am aware of that, thanks - I have changed most of my code but this ones quite a big script so tackling the conversion is a bit lower on my priority list… paid customers sites are the first
and its run on a controlled server so can control which php it has
The problem is that there isnt a standardization for phone numbers worldwide.
HOWEVER;
The list given by felgall is NOT ambiguous.
If the second digit is a 1, the number belongs to the North American Numbering Plan, and therefor the next 3 digits refer to an area code. (Yes, the Bahamas is an Area Code of north american phone numbers.)
If the second digit is a 7, the number belongs to Russia or Kazakhstan.
Other than that, you’re on an expanding sweep:
Take the second two digits. Do they correspond to a country code? If no, take the first three digits, and they are your country code. [It was about at this point that some bright spark piped up and said “But what if there’s more than 100 countries in the world?”]
There are no existing 4 digit “country codes”. (Countries under the NANP are all considered to belong to a singular ‘country’ for purposes of dialing. 1 + Area Code)
It’s not pretty, but it can be done.
<?php
$tdccs = array("Egypt" => 20,"South Africa" => 27, "Greece" => 30,.....); //Array contains 44 entries.
$phonenumber = "113209123098" //Random number string i typed in. No idea if it's a real phone number. If it is, pure coincidence.
switch $phonenumber[1] {
case 1:
//NANP number. Lookup digits 2-4 for location identification.
$country = "NANP";
break;
case 7:
$country = ($phonenumber[2] == 6 || $phonenumber[2] == 7) ? "Kazakhstan" : "Russia";
break;
case default:
$country = array_search($phonenumber[1].$phonenumber[2],$tdccs);
if ($country === false) {
//Country is a 3 digit country code.
}
break;
}
echo $country;
?>
This would tell me my phone number is a NANP number. Specifically on lookup, you’d find it’s a number in Minnesota.
[Hint: You can apply the logic I use for the default case to whittle down the NANP numbers as well. There are 24 NANP area codes for all of the territories/islands in the NANP. There are 23 NANP area codes for Canada. Everything else would be the US.]