phonesphone - phone validation according to NANPA specifications DescriptionFunction bool validatePhone(string $phone [, mixed $filter]) validate the phone number with an optional filter to remove specific types of phone numbers. Function formatPhone(string &$phone [, string $delimiter [, bool $country_code]]) format the phone number. delimiter defaults to hyphen (-) and country code defaults to FALSE. Parametersphone The phone number to be acted uponfilter Specific name of a filter to make sure the phone number does not match. Allowed options are toll_free, toll, toll_all, service, carrier, special, or all. This can be passed as a string or an array of filters.delimiter The character you want to use between the NPA, NXX, and XXXX of the phone number.country code Set to TRUE to include the 1 on the front of the phone number, otherwise set to FALSE. Return ValuesvalidatePhone Returns TRUE if the phone number is valid, FALSE otherwise. formatPhone Sets the class variables phone_number and extension. ExamplesExample 1: Validate a phone number <?php
include('phones.php');
$validate = new Phones;
$phone = '+1 285 555 1213 ext 27';
$phone_number = '+1 258 555 1213 ext 27';
if(list($phone, $ext) = $validate->validatePhone($phone_number, array('toll', 'special'))){
echo $phone.' is VALID';
} else {
echo $phone.' is INVALID';
}
?>Example 2: Validate phone number filtering for toll and special numbers <?php
include('phones.php');
$validate = new Phones;
$phone_number = '+1 258 555 1213 ext 27';
if(list($phone, $ext) = $validate->validatePhone($phone_number)){
echo $phone.' is VALID';
} else {
echo $phone.' is INVALID';
}
?>Exmaple 3: Format a phone number using a period <?php
include('phones.php');
$validate = new Phones;
$phone_number = '+1 258 555 1213 ext 27';
list($phone, $ext) = $validate->formatPhone($phone_number, '.');
echo $phone.' ext.'.$ext;
?>AboutThe North American Numbering Plan (NANPA) defines how phone numbers are assigned. Based on the rules assigned by NANPA, I have created a class that will validate any NANPA specific phone number. Note that this also includes Canada as they are part of NANPA. I have also included some basic filtering methods that will check for specific types of numbers as well. VALID : any phone number that passes the NANPA requirements that is assignable now or in the future (i.e. 555-1212 is pre-assigned as Information similar to 411). This does not exclude area codes or exchanges that may not be in use. The Phones class will validate any North American Numbering Plan phone number as outlined by the governing body NANPA. The purpose of this class is to validate whether or not the phone number in question is assignable. There are also methods that will allow for specific types of filtering. SummaryThe validation routine will by default check for all of the following. There is one acception. Reference Information
NPA (Area) Codes
Central Office Codes (exchanges, prefixes, or simply NXXs)
Carrier Identification Codes (CIC) XXXX
NXX-XXXX Combinations (Phone Number)
Summary of CleansingPhone numbers are written using many different delimiters and many combination's thereof. After looking into the many different approaches available for validating a phone number with all of these various combination's, the task began to become not only burdensome, but incomplete. While it is possible to account for the majority of common formats, I found that on occasion a format would pop up that was not accounted for and the valid number it contained would be flagged as invalid. This prompted me to write a simple but effective method for cleansing a phone number. This method will check for an extension and separate the phone number based on that extension. It will then remove all non-digit characters. This effectively should leave an array of 1) a 10d or 1+10d phone number and 2) an optional extension which can then be verified. If there was no extension, then the section variable in the array will be empty. Summary of FiltersThis class will validate a phone number. However, there are business rules that may require that certain numbers be considered invalid. These numbers should also be flagged. For example, if the business requirements indicate that the phone number must be residential and toll free numbers are not likely to appear as a phone number for a residence, then toll free numbers should be flagged as invalid. I have established the following filters with their accompanying names:
Summary of Format PhoneOnce the phone is validated and filtered, you may want to format it with delimiters to make it easier to read. I have created this method to use the most common format in North America which is (in my opinion): NPA-NXX-XXXX or 1-NPA-NXX-XXXX However, I realize that some people prefer to have their phone numbers formatted using different delimiters, so I made the delimiter a variable. All you do is pass the method the phone and the delimiter and the method will return the formatted phone number. If you only send the phone number, it will use - as default. I have also created the ability to add or remove the Country Code which is the number one (1) to all numbers. The third option you can pass to the method is the bool tue or false. If true, the number will come back as 1-NPA-NXX-XXXX. If false, then the 1- will be removed. |