Giter VIP home page Giter VIP logo

php-phone's Introduction

php-phone

The PHP port of node-phone.

Input & Output

Input

  • phone number: The mobile phone number to be validated and be normalized.
  • country name (optional): The name of the country which the given number should belong to. It can be the following three forms:
    • ISO3166 alpha-2 code
    • ISO3166 alpha-3 code
    • ISO3166 country name

For clarification of ISO-3166 terms, please check the Reference section below.

Output

  • Success: An array containing the following two elements
    • 0: E.164-formated phone number. I.e. +(country code)(phone number)
    • 1: alpha3 code of the country
  • Error: An empty array array()

Algorithm

  1. Search for the associated ISO3166 identifiers and mobile number spec entry. Denoted as spec entry in short in the following paragraphs.
    1. If a country name is given, use it to search for spec entry.
    2. If no country name is given while the phone number is prefixed with a + sign, search for spec entry by trying to match the mobile prefixes for each spec entry.
    3. If neither the two above, take it as USA.
    4. Note that the spec entry contains the following field:
      • ISO3166 alpha-2 code
      • ISO3166 alpha-3 code
      • ISO3166 country name
      • Possible mobile number prefixes
      • Possible mobile number lengths
  2. Remove all non-digit characters in the phone number.
  3. Apply location-specific preprocessing routines to the number. More specifically,
  4. Validate the processed number via the spec entry
    • Check if the length is in the possible mobile number lengths.
    • Check if the number's prefix is in the possible mobile number prefixes.
  5. If it is considered valid, return the final number as +(country code)(proccessed number) and the alpha-3 code in the spec entry.

Test

We use PHPUnit for unit testing. After installing PHPUnit, execute the following command under the project root to launch the test suite:

phpunit --bootstrap src/Mobile_Validator.php ./tests

References

How to call abroad
Read this to get a general knowledge about how to call abroad. Also, the following important terms: country code, exit code, trunk code.

ITU E.164 Official Documentation
Reading of its Annex A section is recommended for the rule clarification.

Telephone Numbering Plan from Wikipedia
Here is a cite that especially relevent:

The International Telecommunication Union (ITU) has established a comprehensive numbering plan, designated E.164, for uniform interoperability of the networks of its member state or regional administrations. It is an open numbering plan, however, imposing a maximum length of 15 digits to telephone numbers. The standard defines a country calling code (country code) for each state or region which is prefixed to each national numbering plan telephone number for international destination routing.

List of mobile phone number series by country
This is the only organized table I found about prefixes of mobile phone number in different countries. However, whether it is reliable is remained in questions.

ISO 3166
Note that the country code of ISO3166 has nothing to do with the country code of E.164. The former is internationally-recognized identifiers for countries, which can be:

  • two letters (alpha-2)
  • three letters (alpha-3)
  • three digit numeric code (numeric-3)

For example, the country code of United States is US for alpha-2, USA for alpha-3, and 840 for numeric-3.

php-phone's People

Contributors

southp avatar

Watchers

Victor Okech avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.