You are here

function format_ph_phone_number in Phone 7

Same name and namespace in other branches
  1. 6 phone.ph.inc \format_ph_phone_number()

Convert a valid Philippine phone number into standard +63 (2) 123-4567 or +63 (2) 123-4567 loc. 123 or mobile +63 (919) 123-4567

Parameters

$phonenumber must be a valid ten-digit number (with optional extension):

File

include/phone.ph.inc, line 64
CCK Field for Philippine phone numbers.

Code

function format_ph_phone_number($phonenumber, $field = FALSE) {
  $area = $number = $extension = $description = '';

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

  //If there are some spaces in the number assume some level of preformatting
  $regex = "/\n                # 5 digit area code.\n                (\n                    (\\d{5}) # capture 5 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?|\\s*)?     # ignore optional separator\n                    (\\d{4}) # capture second set of numbers in the local number\n                |\n                # 4 digit area code.\n                    (\\d{4}) # capture 4 digit area code\n                    (\\s*)?    # ignore required seperator\n                    (\\d{3}) # capture first set of numbers in the local number\n                    (\\S?|\\s*)?     # ignore possible boundary\n                    (\\d{4}) # 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?|\\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 seperator\n                    (\\d{3}) # capture first set of numbers in the local number\n                    (\\S?|\\s*)?     # ignore possible boundary\n                    (\\d{4}) # capture second set of numbers in the local number\n                |\n                # 1 digit area code.\n                    (\\d{1}) # capture 1 digit area code\n                    (\\s*)?     # ignore required boundary to make a distinction with other area codes\n                    (\\d{3}) # capture first set of numbers in the local number\n                    (\\S?|\\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*loc\\.\\s*|\\s*ext\\.\\s*)?\n                (\\d*)?\n\t\t\t\t\t\t\t\t([a-zA-Z0-9\\-\\. \\/\\,\\']{0,})?\n            /x";
  preg_match($regex, $phonenumber, $matches);
  $area = $matches[2] . $matches[7] . $matches[12] . $matches[17] . $matches[22];
  $number = $matches[4] . $matches[9] . $matches[14] . $matches[19] . $matches[24] . '-' . $matches[6] . $matches[11] . $matches[16] . $matches[21] . $matches[26];
  $extension = $matches[28];
  $description = $matches[29];
  $phonenumber = '+63 (' . $area . ') ' . $number;
  $phonenumber .= empty($extension) ? '' : " loc. {$extension}";
  $phonenumber .= empty($description) ? '' : " {$description}";
  return $phonenumber;
}