You are here

function valid_nz_phone_number in Phone 7

Same name and namespace in other branches
  1. 6 phone.nz.inc \valid_nz_phone_number()

Verification for New Zealand Phone Numbers. As supplied by http://www.itu.int/itudoc/itu-t/number/n/nzl/76195_ww9.doc, April 2009

Parameters

string $phonenumber:

Return value

boolean returns boolean FALSE if the phone number is not valid.

File

include/phone.nz.inc, line 23
CCK Field for New Zealand phone numbers.

Code

function valid_nz_phone_number($phonenumber) {

  //$phonenumber = trim($phonenumber);

  // strip formatting chars
  $phonenumber = preg_replace('/[\\-() ]/', '', $phonenumber);

  // strip optional '+64' or '0' prefixes
  $phonenumber = preg_replace('/^(\\+64|0)/', '', $phonenumber);

  //$rules[] = array("Prefix","Minimum length","Maximum length");

  // special purpose service codes and numbers

  /*// Enable if required
    $rules[] = array('10', 3, 3);
    $rules[] = array('12', 6, 7); // NZ Direct Service
    $rules[] = array('60', 4, 4); // Directory Assistance (operator only) - Maybe shouldn't be in here
    $rules[] = array('83', 5, 8); // "Enhanced Voice Services"
    $rules[] = array('86', 8, 8); // "Enhanced Paging Services"
    $rules[] = array('87', 4, 8); // "PSTN access to data services"
    */

  // Mobile
  $rules[] = array(
    '20',
    9,
    9,
  );

  // mobile radio / Orcon mobile
  $rules[] = array(
    '22',
    8,
    10,
  );

  // NZ Communications (actual lengths subject to change)
  $rules[] = array(
    '210',
    8,
    10,
  );

  // Vodafone
  $rules[] = array(
    '211',
    8,
    9,
  );

  // Vodafone
  $rules[] = array(
    '212',
    8,
    9,
  );

  // Vodafone
  $rules[] = array(
    '213',
    8,
    9,
  );

  // Vodafone
  $rules[] = array(
    '214',
    8,
    9,
  );

  // Vodafone
  $rules[] = array(
    '215',
    8,
    9,
  );

  // Vodafone
  $rules[] = array(
    '216',
    8,
    9,
  );

  // Vodafone
  $rules[] = array(
    '217',
    8,
    9,
  );

  // Vodafone
  $rules[] = array(
    '218',
    8,
    9,
  );

  // Vodafone
  $rules[] = array(
    '219',
    8,
    9,
  );

  // Vodafone
  $rules[] = array(
    '24',
    8,
    8,
  );

  // Scott Base

  //$rules[] = array('25', 8, 9); // Old Telecom 025, now unused
  $rules[] = array(
    '26',
    8,
    9,
  );

  // Telecom Paging
  $rules[] = array(
    '27',
    9,
    9,
  );

  // Telecom 027 mobile
  $rules[] = array(
    '29',
    8,
    9,
  );

  // TelstraClear mobile
  // South Island regional
  $rules[] = array(
    '32',
    8,
    8,
  );
  $rules[] = array(
    '33',
    8,
    8,
  );
  $rules[] = array(
    '34',
    8,
    8,
  );
  $rules[] = array(
    '35',
    8,
    8,
  );
  $rules[] = array(
    '36',
    8,
    8,
  );
  $rules[] = array(
    '37',
    8,
    8,
  );
  $rules[] = array(
    '39',
    8,
    8,
  );

  // Wellington regional
  $rules[] = array(
    '42',
    8,
    8,
  );
  $rules[] = array(
    '43',
    8,
    8,
  );
  $rules[] = array(
    '44',
    8,
    8,
  );
  $rules[] = array(
    '45',
    8,
    8,
  );
  $rules[] = array(
    '46',
    8,
    8,
  );
  $rules[] = array(
    '48',
    8,
    8,
  );
  $rules[] = array(
    '49',
    8,
    8,
  );

  // Manawatu, Taranaki, Hawkes Bay, Gisborne, Wairarapa, Otaki regional
  $rules[] = array(
    '62',
    8,
    8,
  );
  $rules[] = array(
    '63',
    8,
    8,
  );
  $rules[] = array(
    '67',
    8,
    8,
  );
  $rules[] = array(
    '68',
    8,
    8,
  );
  $rules[] = array(
    '69',
    8,
    8,
  );

  // Waikato, BOP, Taumarunui regional
  $rules[] = array(
    '73',
    8,
    8,
  );
  $rules[] = array(
    '75',
    8,
    8,
  );
  $rules[] = array(
    '62',
    8,
    8,
  );
  $rules[] = array(
    '78',
    8,
    8,
  );
  $rules[] = array(
    '79',
    8,
    8,
  );

  // Freecall
  $rules[] = array(
    '80',
    8,
    10,
  );

  // Pay-call
  $rules[] = array(
    '90',
    8,
    10,
  );

  // Auckland + Northland regional
  $rules[] = array(
    '92',
    8,
    8,
  );
  $rules[] = array(
    '93',
    8,
    8,
  );
  $rules[] = array(
    '94',
    8,
    8,
  );
  $rules[] = array(
    '95',
    8,
    8,
  );
  $rules[] = array(
    '96',
    8,
    8,
  );
  $rules[] = array(
    '98',
    8,
    8,
  );
  $rules[] = array(
    '99',
    8,
    8,
  );
  foreach ($rules as $rule) {
    if (preg_match('/^' . $rule[0] . '/', $phonenumber) && strlen($phonenumber) >= $rule[1] && strlen($phonenumber) <= $rule[2]) {
      return true;
    }
  }
  return false;
}