You are here

phone.es.inc in Phone 5

Same filename and directory in other branches
  1. 6 phone.es.inc

File

phone.es.inc
View source
<?php

/**
 * Verifies that $phonenumber is a valid nine-digit Spanish phone number
 *
 * @param string $phonenumber
 * @return boolean Returns boolean FALSE if the phone number is not valid. 
 */
function valid_es_phone_number($phonenumber) {

  //$phonenumber = trim($phonenumber);

  // define regular expression
  $regex = "/\n    \\D*           # optional separator \n    [69]\\d{2}     # first group of numbers\n    \\D*           # optional separator\n    \\d{3}         # second group\n    \\D*           # optional separator\n    \\d{3}         # third group\n    \\D*           # ignore trailing non-digits\n    \$/x";

  // return true if valid, false otherwise
  return (bool) preg_match($regex, $phonenumber);
}

/**
 * Convert a valid Spanish phone number into standard (+34) 916 555 777 format
 * 
 * @param $phonenumber must be a valid nine-digit number (with optional international prefix)
 * 
 */
function format_es_phone_number($phonenumber) {

  // define regular expression
  $regex = "/\n    \\D*           # optional separator \n    ([69]\\d{2})   # first group of numbers\n    \\D*           # optional separator\n    (\\d{3})       # second group\n    \\D*           # optional separator\n    (\\d{3})       # third group\n    \\D*           # ignore trailing non-digits\n    \$/x";

  // get digits of phone number
  preg_match($regex, $phonenumber, $matches);

  // construct ten-digit phone number
  $phonenumber = $matches[1] . ' ' . $matches[2] . ' ' . $matches[3];
  return $phonenumber;
}

Functions

Namesort descending Description
format_es_phone_number Convert a valid Spanish phone number into standard (+34) 916 555 777 format
valid_es_phone_number Verifies that $phonenumber is a valid nine-digit Spanish phone number