Thursday, November 17, 2011

Pakistan's Telephone Numbers Validation

/*
 +===================================================================
 | @title: Pakistan's Telephone numbers Validation
 
| @author: Sag-e-Attar Junaid Atari <mj.atari@gmail.com> 
 | @info: http://en.wikipedia.org/wiki/Telephone_numbers_in_Pakistan
 +===================================================================

 !!! PATTERNS TYPES | SUPPORTED FORMATES !!!

 @ Premium Number
 # Toll Free: 0800 00000 | 0800 000 00 | 080000000
 # Premium Rate: 0900 00000 | 0900 000 00 | 090000000

 @ Mobile Number
 > Mobile code only start with 3
 # Format 1:  (0092)-331-5776662
 # Format 2:  (0092)-331-577 6662
 # Format 2:  (0092) 331 577 6662
 # Format 3:  (+92) 345 5776662
 # Format 4:  (+92)-345-577 6662
 # Format 5:  (+92) 345 577 6662
 # Format 6:  (+92)3455776662
 # Format 7:  +923455776662
 # Format 8:  +92-345-5776662
 # Format 9:  +92 345 577 6662
 # Format 10: 0092-345-577 6662
 # Format 11: 03455776662
 # etc ...

 @ Landline Fixed number
 > Area code cannot start with 3
 # Format 1:  0092 50 000 0000
 # Format 2:  (0092)50 000 0000
 # Format 3:  009250000 0000
 # Format 4:  0092500000000
 # Format 5:  +92 50 000 0000
 # Format 6:  (+92) 50 000 0000
 # Format 7:  +92 50 000 0000
 # Format 8:  +9250 000 0000
 # Format 9:  +9250000 0000
 # Format 10: +9250000 0000
 # Format 11: +9250 0000000
 # Format 12: 0500000000
 # Format 13: 051 0000000
 # Format 14: 061 00000000
 # Format 15: 061 000 000 00
 # Format 16: 0600 000 000
 # Format 17: 0600 000000
 # Format 18: 0600000000
 # Format 19: +92700000000
 # Format 20: 0092600000000
 # Format 21: 0092 600 000 000
 # Format 22: 0092 600000 000
 # Format 23: 0092 600000000
 # etc ...
*/

/*
 * Validate the Pakistan telephone numbers.
 *
 * @author    Junaid Atari <mj.atari@gmail.com>
 * @version   1.0
 * @param     string   $telNumber    Tele number to valid.
 * @return    bool     TRUE on valid | else FALSE
*/

function isPkTelePhoneNumber $telNumber )
{
    if ( !
is_string $telNumber )
         && !
is_int $telNumber ) )
            return 
false;

    
/* All regex patterns written by Sag-e-Attar Junaid Atari */
    
$patterns = array (

        
'premiumNumber'  => '/^0(8|9)00 ?[0-9]{3} ?[0-9]{2}$/'

        
'mobileNumber'   => '/^((\(((\+|00)92)\)|(\+|00)92)(( |\-)?).
                            '(3[0-9]{2})\6|0(3[0-9]{2})( |\-)?)[0-9]' .
                            '{3}( |\-)?[0-9]{4}$/',

        'landlineNumber' => '/^(\((\+|00)92\)( )?|(\+|00)92( )?|0).
                            '[1-24-9]([0-9]{1}( )?[0-9]{3}( )?[0-9].
                            '{3}( )?[0-9]{1,2}|[0-9]{2}( )?[0-9]{3}' .
                            '( )?[0-9]{3})$/'  
    );

    foreach ( 
$patterns as $pattern )
        
$response[] preg_match $pattern$telNumber )
                        ? 
true
                        false;

    return ( 
$response[0] || $response[1] || $response[2] );
}

/*
 +================================
 | Example of usage
 +================================
*/


if ( isPkTelePhoneNumber '0092 600 000 000' ) ) 
    echo 'Valid number.' "\n\n";
else
    echo 
'Invalid Number.' "\n\n";