You are here

function sms_international_validate_number in SMS Framework 7

Same name and namespace in other branches
  1. 6.2 modules/sms_international/sms_international.module \sms_international_validate_number()

Take a number including country code and make sure it is valid.

Parameters

string $number: The number to be validated.

array $options: Validation options.

Return value

bool|string

File

modules/sms_international/sms_international.module, line 19
Contains sms_international module.

Code

function sms_international_validate_number($number, $options = array()) {

  // Replace all non-digit characters with spaces.
  $number = preg_replace("/[^0-9]/", '', $number);
  $number = trim($number);

  // Remove leading zeros.
  $number = ltrim($number, '0');
  $candidates = array();
  for ($i = 0; $i < 4; $i++) {
    $candidates[] = substr($number, 0, $i + 1);
  }
  $result = db_select('sms_international', 's')
    ->fields('s', array(
    'iso',
    'country_code',
    'min_length',
    'max_length',
  ))
    ->condition('country_code', $candidates, 'IN')
    ->condition('status', 1)
    ->execute();
  $countries = array();
  foreach ($result as $country) {
    $countries[] = $country;
    $codes[$country['country_code']] = $country['iso'];
  }
  foreach ($countries as $country) {
    $trimmed_number = ltrim($number, $country['country_code']);
    if (strlen($trimmed_number) >= $country['min_length'] && strlen($trimmed_number) <= $country['max_length']) {
      return FALSE;
    }
  }
  return FALSE;
}