You are here

function format_eg_phone_number in Phone 6

Same name and namespace in other branches
  1. 7 include/phone.eg.inc \format_eg_phone_number()

File

./phone.eg.inc, line 102

Code

function format_eg_phone_number($phonenumber, $field) {
  if (preg_match("/^" . TELEPHONE_REGEX . "\$/x", $phonenumber)) {

    //already in proper format
    return $phonenumber;
  }
  else {

    //remove country code, zeros, and braces
    $phonenumber = preg_replace("/(^(\\+20)?\\s?|\\(0?|\\)|^0?)/", '', $phonenumber);
  }

  //If there are some spaces in the number assume some level of preformatting
  if (preg_match("/ /", $phonenumber)) {
    $regex = "/^\n   \t(\n\t\t\t\t(\\d{1,2})\t\t\t\t\t# area code\n\t\t\t\t\\s*  \t\t\t\t\t\t   # ignore required separator\n\t\t\t\t(\\d{3,4})\t\t\t\t\t# 4 digits in case of Greater Cairo\n\t\t\t\t\\s*\n\t\t\t\t(\\d{4})\n\t\t\t)\n\t\t\t((\\s*\\#)?(\\d*))?  \t\t\t\t\t\t\t\t\t# extension\n\t\t\$/x";
    preg_match($regex, $phonenumber, $matches);
    $area = $matches[2];
    $number = $matches[3] . ' ' . $matches[4];
    $extension = $matches[7];
  }
  else {

    //no spaces?, then apply some guessing
    $regex = "/^  # order is important in this one\n\t\t\t(\n\t\t\t\t\t(\\d)(\\d{4})(\\d{4})\t\t\t\t# 2 area code, followed by 8 digits begining with 2 or 3: Greater Cairo\n\t\t\t\t|\n\t\t\t\t\t(\\d{1,2})(\\d{3})(\\d{4})\t\t\t\t# 1 or 2-digit area code followed by 7 digits: regional or mobile\n\t\t\t)\n\t\t\t((\\s*\\#)?(\\d*))?\n\t\t\$/x";
    preg_match($regex, $phonenumber, $matches);
    $area = $matches[2];
    $number = $matches[3] . ' ' . $matches[4];
    $extension = $matches[5];
  }
  return '+20 (' . $area . ') ' . $number . (empty($extension) ? '' : " #{$extension}");
}