Search code examples
phpphone-number

Determining local / international phone numbers for the US


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..

  • Imagine you are based in the US and are receiving a call on your mobile phone
  • Determine whether the call is from a US or another country
  • In the UK calls can come appear in the local or international format, it should be able to determine if it's a US number or not
  • I believe international calls can be displayed on your phone in the format 0044 or +44 (if you were calling from the UK to the US)
  • If the call is from within the US is it a valid phone number?
  • Strip out all non numerical characters and reformat to national format. For example, the UK number 01530 123123 would get reformatted to 01530123123
  • Strip out all non numerical characters and reformat to international format. For example, the UK number 01530 123123 would get reformatted to 00441530123123

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..


Solution

  • 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)