The PHP port of node-phone.
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.
- 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()
- Search for the associated ISO3166 identifiers and mobile number spec entry. Denoted as spec entry in short in the following paragraphs.
- If a country name is given, use it to search for spec entry.
- 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.
- If neither the two above, take it as USA.
- 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
- Remove all non-digit characters in the phone number.
- Apply location-specific preprocessing routines to the number. More specifically,
- Remove all leading zeros except for GAB, CIV and COG.
- If it is Russian number and begins with 89. Remove the 8.
- 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.
- If it is considered valid, return the final number as +(country code)(proccessed number) and the alpha-3 code in the spec entry.
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
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.