Skip to content

Instantly share code, notes, and snippets.

@snowbob
Created May 21, 2019 08:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snowbob/18887ec93a691dfaa355dbd7f60448d2 to your computer and use it in GitHub Desktop.
Save snowbob/18887ec93a691dfaa355dbd7f60448d2 to your computer and use it in GitHub Desktop.
check if valid mobile phone number
<?php
$phones = array( '447549007599', '353852723831', '614549007599' , '615549007599' , '642549007599' , '918667672071');
#$phones = array( '448549007599');
function isValidMobileNumber($phones = null) {
$countryCode = array( '44' => '7', '353' => '8', '61' =>'4|5', '64' =>'2');
$result = array();
foreach( $phones as $number )
{
$country = array('STATUS' => 'INVALID', 'AREACODE' => '', 'NUMBER' => '');
$country['NUMBER'] = $number;
foreach( $countryCode as $key => $value )
{
if ( substr( $number, 0, strlen( $key ) ) == $key ) {
if ( in_array(substr( $number, strlen( $key ) , 1 ), explode('|', $value)) ) {
$country['STATUS'] = "VALID";
$country['AREACODE'] = $key;
break;
}
}
}
array_push($result, $country);
}
return $result;
}
print_r(isValidMobileNumber($phones));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment