Determining US Local / International Telephone Numbers

You probably think you have been asked about this millions of times, but I think my question is a little different. Most phone number verification requests are used to verify user input. In my scenario, I try to check incoming phone calls to a US mobile phone and determine if they are calling from another country or from the USA.

My main problem is that I am in the UK and have no experience with US phone numbers. I read about how to dial a number (both international and national), but I worry that phone numbers are displayed differently on the recipient's phone and in different formats.

Here is the specification of what I'm trying to do.

  • Imagine that you are in the USA and receive a call to your mobile phone.
  • Determine if the call is from the USA or another country.
  • In the UK, calls may appear in a local or international format, it should be able to determine whether it is a US number or not.
  • I believe that international calls can be displayed on your phone in the format 0044 or +44 (if you called from the UK to the USA)
  • If the call from the USA is a valid phone number?
  • Extract all non-numeric characters and reformat them to the national format. For example, UK number 01530 123123 will be reformatted to 01530123123.
  • Delete all non-numeric characters and reformat them to international format. For example, UK number 01530 123123 will be reformatted to 00441530123123.

I am trying to write code in PHP and avoid regular expressions. I think he could use substr () to break up the parts of the number.

Any thoughts on how to do this, code snippets or examples of phone numbers? I really hope that someone from the USA can educate me on formatting a phone number, etc.

+4
source share
7 answers

People who say they’re just looking for +1 haven’t worked much with phone numbers. Country Code 1 applies to many regions of North America, not just the United States. So what really needs to happen, there is some logic around the tables listed for the North American Numbering Plan and the North American Numbering Code List

Here is a short list of some "exclusive" countries with numbers starting with +1 that are not based on the United States.

Bermudian:

  • After 1995: +1 441 (xxx) xxxx

Puerto Rico:

  • After 2001: +1 787 xxx xxxx or +1 939 xxx xxxx (overlay for the entire island)

US Virgin Islands:

  • After 1997: +1 340 xxx xxxx

Northern Mariana Islands:

  • After 1997: +1 670 xxx xxxx

Guam:

  • After 1997: +1 671 xxx xxxx

American Samoa:

  • After October 2, 2004: +1 684 xxx xxxx

Saint Martin:

  • After joining NANP: +1 721 xxx xxxx (date will be determined)
+3
source

it 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" now we are not paid, free or special numbers

according to wikipedia btw since you have a uk number, we will always have a prefix (otherwise the call itself would not have been possible)

+2
source

As far as I know, numbers always have a country code prefix if they “cross borders,” so it’s good enough to check at +1 or 001 to identify the number in the USA.

But just because you receive a phone call with an American number does not mean that the call comes from the United States. It can be a mobile phone located anywhere in the world.

+1
source

If it starts with + , which is not described by 1 or starts with 011 , it is “international”. The rest is "national."

But, “national” here means North America, not the United States of America. The difference may be important in your case. If you really need to know if this is the United States and not North America, you need to check the area code. In North America, the number of telephones is: +1 (AAA) NNN-NNNN . Where AAA is the area code. This area code can tell you where it comes from.

Here you can get a list of area codes http://en.wikipedia.org/wiki/List_of_North_American_Numbering_Plan_area_codes . Please note that when assigning a regional code, it will always be with this assignment, but some area codes may be added in the future.

EDIT: Area codes 800, 822, 833, 844, 855, 866, 877, 880 - 882, 888 mean free (or reserved for future Toll Free expansion) and cannot be located more accurately than in North America. The only thing you are sure of these numbers is they are free in North America.

+1
source

US numbers that are local start with an area code, and some are of particular importance, such as 555 or 800, and are always equal to the number of digits. Some can be entered as alpha text by a phone user similar to 800CALLNOW, and you would not receive an international call in this format. As for your project, you need to be an authorized and registered mobile network software operator and use standard code modules for your device or collect statistics about calls, and you usually do not need to know the specifics you are asking for, since filtering will be performed in this module, and all you need is to output the variable as a parameter. An organization in the United States that requires work will need to provide you with a specification of any specific number formats that may be encountered, and possibly a document from the national telecommunications regulator, suggesting that work can be done outside the United States altogether. If they have not provided details or cannot do this, if asked, then this part of the project cannot be completed satisfactorily.

0
source

You can try this API, which gives you several phone number formats when ordering a number in the USA. From this you can find out if this is a US number or not. Returns the result in JSON https://www.mashape.com/parsify/format#endpoint-Phone-Number

0
source

We started using the libphonenumber library from Google. This allows you to format your phone numbers in several international formats.

There is a PHP port here: https://github.com/davideme/libphonenumber-for-PHP

0
source

All Articles