Reputation: 10942
You're probably thinking this has been asked a million times before but I think that my question is a little different. Most requests for phone number validation are used for validating user input. In my scenario I'm trying to validating incoming phone calls to a US mobile phone, and determine whether they're calling from another country or from within the US.
My main problem is that I'm based in the UK and have no experience of US phone numbers.. I've read about the format of how to dial etc (both international and national) but my worry is that the phone numbers appear differently on the recipient's phone, and in a variety of formats.
Here is the specification of what I'm trying to do..
I'm trying to write the code in PHP and avoid regular expressions. I'm thinking it could use substr() to break down parts of the number.
Any thoughts on how to go about this, code snippets or example phone numbers? I'm really hoping someone from the US will be able to enlighten me on phone number formatting etc..
Upvotes: 4
Views: 3612
Reputation: 4434
We have begun to use the libphonenumber library from Google. It allows you to format your phonenumbers according to multiple international formats.
There is a PHP port here: https://github.com/davideme/libphonenumber-for-PHP
Upvotes: 0
Reputation: 610
You can try this API which gives you several phone number formats when you supply a US number. From this you can tell if it's a US number or not. Returns result in JSON https://www.mashape.com/parsify/format#endpoint-Phone-Number
Upvotes: 0
Reputation: 932
this would do it
<?php
/**
* function isUsPhone
* {arg} $num string the number
* {returns} bool
**/
function isUsPhone($num){
// for au use this
// foreach(array('+', '0011') as $c){
foreach(array('+', '00') as $c){
if(substr($num,0,strlen($c)+1) == $c.'1'){
foreach(
array(
205, 251, 256, 334, 659, 938, 907, 250, 480, 520, 602, 623, 928, 327, 479, 501, 870, 209, 213, 310, 323, 341, 369, 408, 415, 424, 442, 510, 530, 559, 562, 619, 626, 627, 628, 650, 657, 661, 669, 707, 714, 747, 760, 764, 805, 818, 831, 858, 909, 916, 925, 935, 949, 951 , 303, 719, 720, 970 , 203, 475, 860, 959, 302, 202, 239, 305, 321, 352, 386, 407, 561, 689, 727, 754, 772, 786, 813, 850, 863, 904, 941, 954 , 229, 404, 470, 478, 678, 706, 762, 770, 912 , 808, 208, 217, 224, 309, 312, 331, 447, 464, 618, 630, 708, 730, 773, 779, 815, 847, 872, 219, 260, 317, 574, 765, 812, 319, 515, 563, 641, 712 , 316, 620, 785, 913, 270, 364, 502, 606, 859, 225, 318, 337, 504, 985, 207, 227, 240, 301, 410, 443, 667, 339, 351, 413, 508, 617, 774, 781, 857, 978 , 231, 248, 269, 313, 517, 586, 616, 679, 734, 810, 906, 947, 989 , 218, 320, 507, 612, 651, 763, 952, 228, 601, 662, 769, 314, 417, 557, 573, 636, 660, 816, 975, 406, 308, 402, 531, 702, 775, 603, 201, 551, 609, 732, 848, 856, 862, 908, 973 , 505, 575, 212, 315, 347, 516, 518, 585, 607, 631, 646, 716, 718, 845, 914, 917, 929 , 252, 336, 704, 828, 910, 919, 980, 984 , 701, 216, 234, 283, 330, 380, 419, 440, 513, 567, 614, 740, 937, 405, 539, 580, 918, 458, 503, 541, 971, 215, 267, 272, 412, 445, 484, 570, 582, 610, 717, 724, 814, 835, 878 , 401, 803, 843, 864, 605, 423, 615, 731, 865, 901, 931, 210, 214, 254, 281, 325, 361, 409, 430, 432, 469, 512, 682, 713, 737, 806, 817, 830, 832, 903, 915, 936, 940, 956, 972, 979 , 385, 435, 801, 802, 276, 434, 540, 571, 703, 757, 804 , 206, 253, 360, 425, 509, 564, 304, 681, 262, 274, 414, 534, 608, 715, 920, 307
) as $a
){
if(substr($num,strlen($c)+1,3) === (string) $a){
echo "\n".substr($num,strlen($c)+1,3)."\n";
return true;
}
}
}
}
return false;
}
var_dump(isUsPhone('+18003343423'));
?>
"us" is us now this an no toll, tollfree, or special numbers
acc to wikipedia b.t.w. as you have a uk number, us will always have a prefix (otherwise the call itself would not be possible)
Upvotes: 2
Reputation: 53536
The people saying to just look for +1 have not worked with telephone numbers much. The 1 country code applies to many areas of North America not just the US. So what really needs to happen is some logic around the tables listed for the North American Numbering Plan and at the List of North American Numbering Plan area codes
Here is a short list of some "exceptional" countries that have numbers starting in +1 that are not US based.
Bermuda:
- After 1995: +1 441 (xxx) xxxx
Puerto Rico:
- After 2001: +1 787 xxx xxxx or +1 939 xxx xxxx (overlay for entire island)
US Virgin Islands:
- After 1997: +1 340 xxx xxxx
Northern Marianas:
- After 1997: +1 670 xxx xxxx
Guam:
- After 1997: +1 671 xxx xxxx
American Samoa:
- After October 2, 2004: +1 684 xxx xxxx
Saint Maarten:
- After joining NANP: +1 721 xxx xxxx (date to be determined)
Upvotes: 3
Reputation: 126
US numbers which are local start with an area code and some have special significance like 555 or 800 and are always the same number of digits in total. Some may be input as alpha text by the phone user similar to 800CALLNOW and you would not get an international call in that format. Regarding your project you would need to be an authorised and registered mobile network operating software engineer and using standard modules of code for a device or gathering statistics about calls and you would not generally need to have knowledge of the specifics you ask about as the filtering would be done in that module and all you need is the output to a variable as a parameter. The organization in the US requesting the work would need to supply you with a specification of any specific number formats that may be encountered and maybe a document from the national telecomms regulator assuming the work can be done outside the US at all. If they have not supplied the details or can not do that if asked then that part of the project can not be completed satisfactorily.
Upvotes: 0
Reputation: 304
If it begin with + not folllowed by 1 or it begin with 011 it is "international". Other else it is "national".
But, "national" here means North America and not United States of America. The difference might be important in your case. If you really need to know if it is United State and not North America you have to check the regional code. In North America, phones number are composed like that +1 (AAA) NNN-NNNN Where AAA is the area code. This area code can tell You where it is coming from.
You can get a list of area codes here http://en.wikipedia.org/wiki/List_of_North_American_Numbering_Plan_area_codes . Note that when an area code is assigned it will always be at this assignement, but some area codes could be added in future.
EDIT : Area codes 800, 822, 833, 844, 855, 866, 877, 880 through 882, 888 means Toll Free (or reserved for future expansion of Toll Free) and cannot be located more precisly than North America. The only things you are sure about these numbers, they are toll free in North America.
Upvotes: 1
Reputation:
As far as I know numbers are always prefixed with the country code if they "cross borders", so it should be pretty safe to check for +1 or 001 to identify a US number.
But just because you receive a phone call with a US number, doesn't mean the call is coming from the US. It could be a mobile phone that is located anywhere in the world.
Upvotes: 1