Simple Mobile Number Validator

This function is meant to be used in a loop to prepare mobile phone numbers to be used as inputs where they are expected to be in the full international format without IDD Prefix (i.e. 00, 011, 0010, or +), for example 966555555555, but where the provided number has not necessarily been validated upon entry.

With the default settings (for Saudi Arabia), mobile phone numbers that will be accepted include:

  • Correct numbers as dialed locally with preceding zero:
    e.g. 0555555555
  • Correct numbers without preceding zero:
    e.g. 555555555 (perhaps coming from Excel)
  • Correct numbers already in international format with or without preceding plus sign or Saudi IDD:
    e.g. 00966555555555, 966555555555 or +966555555555

Invalid numbers will simply be omitted.

<?php
/**
 * Formats mobile numbers as international.
 * 
 * @access public
 * @param string $number is a mobile phone number
 * @param array $args is an associative array of variables (see $defaults)
 * @return string
 */
 
function format_mobile_as_international( $number, $args=null ) {
	$defaults = array(
		'Country Code' => '966', // (966) 555555555
		'Local Prefix' => '0',   // (0) 555555555
		'First Digit' => '5', // 0 (5) 55555555
		'IDD' => '00', // (00) 966 555555555 
		'Local Length' => 10 // length as dialled locally e.g. 0555555555 = 10 digits
	);
	$args = array_merge( $defaults, (array) $args );
	
	// only keep digits: no spaces, dashes, plus signs, etc.
	$number = preg_replace( "/[^0-9]/", "", $number ); 
	
	// Phone number is like 0555555555	
	if ( $number[0] == $args['Local Prefix'] && strlen( $number ) == (int) $args['Local Length'] ) {
		$number = substr( $number, strlen( $args['Local Prefix'] ) );
		$result = $args['Country Code'] . $number;
	}
	// Phone number is like 555555555
	elseif ( $number[0] == $args['First Digit'] && strlen( $number ) == (int) $args['Local Length'] - strlen( $args['First Digit'] ) ) {
		$result = $args['Country Code'] . $number;
	}
	// Phone number is like 00966555555555
	elseif ( substr( $number, 0, strlen( $args['IDD'] ) ) == $args['IDD'] && strlen( $number ) == (int) $args['Local Length'] - strlen( $args['Local Prefix'] ) + strlen( $args['IDD'] ) + strlen( $args['Country Code'] ) ) { 
		$result = substr( $number, strlen( $args['IDD'] ) );
	} 
	// Phone number is like 966555555555
	elseif ( substr( $number, 0, 3 ) == $args['Country Code'] && strlen( $number ) == (int) $args['Local Length'] - strlen( $args['Local Prefix'] ) + strlen( $args['Country Code'] ) ) { 
		$result = $number;
	} 
	// else omit
		
	return $result;
}

echo format_mobile_as_international("00966555555555");
?>

By overriding the defaults, it can be used for any country’s phone numbers. You can change the defaults directly in the function, or for more flexibility, pass an associative array as the second argument to override the defaults.

 '971' );
echo format_mobile_as_international("+971555555555", $args);
?>

An example of how this might be used is to insert an appropriately formatted mobile phone number into a url for sending an SMS or WhatsApp message.

 '055 5555555',
	'Anna' => '+966123456789',
	'Phil' => '(966)512345678',
	'BAD' => '055555555', // will be omitted
	'Maha' => '055.000.0000'
);

foreach ( $contacts as $name => $number ) {
	$mobile = format_mobile_as_international( $number );
	if ( $mobile ) {
		echo '

Send WhatsApp Message to ' . $name . '

'; } } ?>

2 Comments

Add Yours →

Leave a Reply