You are here

phone.uk.inc in Phone 5

File

phone.uk.inc
View source
<?php

/**
 * Verifies that $phonenumber is a valid eleven-digit United Kingdom phone number
 *
 * Regular expression adapted from Amos Hurd's regex at RegExLib.com
 *
 * @param string $phonenumber
 * @return boolean Returns boolean FALSE if the phone number is not valid. 
 */
function valid_fr_phone_number($phonenumber) {

  //$phonenumber = trim($phonenumber);
  if (!preg_match("/^(\\+){0,1}([0-9 ])*[0-9]\$/i", $phonenumber)) {
    return false;
  }
  else {
    return true;
  }
}
function valid_uk_phone_number($phonenumber) {

  /*
    Accepts:
        +441970123456
        +44(0)1970123456
        +44 1970 123 456
        +44 (0)1970 123 456
        (01970) 123456 #0001
    Rejects:
        (+441970)123456
        +44(1970)123456
        +44 01970 123 456
        (0197) 0123456 #01
  */
  $regex = "/\n    (\n        (^\\+44\\s?(\\(0\\))?\\d{4}|^\\(?0\\d{4}\\)?){1}\\s?\\d{3}\\s?\\d{3}  # 4 digit area code with optional +44 internationalisation or not, optional spaces and brackets.\n        |\n        (^\\+44\\s?(\\(0\\))?\\d{3}|^\\(?0\\d{3}\\)?){1}\\s?\\d{3}\\s?\\d{4}  # 3 digit area code with optional +44 internationalisation or not, optional spaces and brackets.\n        |\n        (^\\+44\\s?(\\(0\\))?\\d{2}|^\\(?0\\d{2}\\)?){1}\\s?\\d{4}\\s?\\d{4}  # 2 digit area code with optional +44 internationalisation or not, optional spaces and brackets.\n    )\n    (\\s?\\#\\d*)?   # optional extension number shown with a hash divider\n  /x";
  if (!preg_match($regex, $phonenumber)) {
    return false;
  }
  else {
    return true;
  }
}

/**
 * Convert a valid United Kingdom phone number into standard +44 (0)1970 123 456 #001 international format
 * 
 * @param $phonenumber must be a valid eleven-digit number (with optional extension)
 * 
 */
function format_uk_phone_number($phonenumber) {
  $area = $number = $extension = '';

  //If we already have the formatting we want just return
  if (preg_match("/\n        (\n            \\+44\\s\\(0\\)\\d{4}\\s\\d{3}\\s\\d{3}  # 4 digit area code\n            |\n            \\+44\\s\\(0\\)\\d{3}\\s\\d{3}\\s\\d{4}  # 3 digit area code\n            |\n            \\+44\\s\\(0\\)\\d{2}\\s\\d{4}\\s\\d{4}  # 2 digit area code\n        )\n        (\\s\\#\\d*)?\n    /", $phonenumber)) {
    return $phonenumber;
  }
  else {

    //Simplify to 10 digit number and clean up ready for international reformat.
    $phonenumber = preg_replace("/^(\\+44)?\\s?(\\(?0\\)?)?/", "", $phonenumber);
    $phonenumber = preg_replace("/\\(/", "", $phonenumber);
    $phonenumber = preg_replace("/\\(0/", "", $phonenumber);
    $phonenumber = preg_replace("/\\)/", "", $phonenumber);

    //If there are some spaces in the number assume some level of preformatting
    if (preg_match("/ /", $phonenumber)) {
      $regex = "/\n                # 4 digit area code.\n                (\n                    (\\d{4}) # capture 4 digit area code\n                    \\s+     # ignore required separator to make a distinction with other area codes\n                    (\\d{3}) # capture first set of numbers in the local number\n                    \\s*     # ignore optional separator\n                    (\\d{3}) # capture second set of numbers in the local number\n                |\n                # 3 digit area code.\n                    (\\d{3}) # capture 3 digit area code\n                    \\s+     # ignore required seperator\n                    (\\d{3}) # capture first set of numbers in the local number\n                    \\s*     # ignore possible boundary\n                    (\\d{4}) # capture second set of numbers in the local number\n                |\n                # 2 digit area code.\n                    (\\d{2}) # capture 2 digit area code\n                    \\s+     # ignore required boundary to make a distinction with other area codes\n                    (\\d{4}) # capture first set of numbers in the local number\n                    \\s*     # ignore possible boundary\n                    (\\d{4}) # capture second set of numbers in the local number\n                )\n                # capture the optional extension number\n                (\\s*\\#)?\n                (\\d{4}|\\d{3})?\n            /x";
      preg_match($regex, $phonenumber, $matches);
      var_export($matches);
      $area = $matches[2] . $matches[5] . $matches[8];
      $number = $matches[3] . $matches[6] . $matches[9] . ' ' . $matches[4] . $matches[7] . $matches[10];
      $extension = $matches[12];
    }
    else {
      preg_match("/(\\d{4})(\\d{3})(\\d{3})\\#?(\\d*)?/", $phonenumber, $matches);
      $area = $matches[1];
      $number = $matches[2] . ' ' . $matches[3];
      $extension = $matches[4];
    }
    $phonenumber = '+44 (0)' . $area . ' ' . $number;
    $phonenumber .= empty($extension) ? '' : " #{$extension}";
  }
  return $phonenumber;
}

Functions

Namesort descending Description
format_uk_phone_number Convert a valid United Kingdom phone number into standard +44 (0)1970 123 456 #001 international format
valid_fr_phone_number Verifies that $phonenumber is a valid eleven-digit United Kingdom phone number
valid_uk_phone_number