diff --git a/include/phone.es.inc b/include/phone.es.inc
index 8558044..e49886a 100644
--- a/include/phone.es.inc
+++ b/include/phone.es.inc
@@ -5,13 +5,18 @@
* CCK Field for Spanish phone numbers.
*/
-function phone_es_metadata() {
- // These strings are translated using t() on output.
- return array(
- 'error' => '"%value" is not a valid Spanish phone number
Spanish phone numbers should only contains numbers and spaces and be like 999 999 999',
- );
- }
+/**
+ * According to this http://en.wikipedia.org/wiki/Telephone_numbers_in_Spain
+ * these are the rules for Spanish telephone number.
+ */
+define('PHONE_ES_REGEX', "/^(\+|0{2}|)?(34|0|)[\s.-]?([1-9][0-9]{1,2})[\s.-]?([1-9][0-9]{2})[\s.-]?([1-9][0-9]{2})$/");
+function phone_es_metadata() {
+ // These strings are translated using t() on output.
+ return array(
+ 'error' => '"%value" is not a valid Spanish phone number
Spanish phone numbers should only contains numbers and spaces and be like 999 999 999 or 99 999 999',
+ );
+}
/**
* Verifies that $phonenumber is a valid nine-digit Spanish phone number
@@ -20,25 +25,9 @@ function phone_es_metadata() {
* @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 = "/
- // \D* # optional separator
- // [69]\d{2} # first group of numbers
- // \D* # optional separator
- // \d{3} # second group
- // \D* # optional separator
- // \d{3} # third group
- // \D* # ignore trailing non-digits
- // $/x";
-
- $regex = '/^[0-9]{2,3}-? ?[0-9]{6,7}$/';
-
-
// return true if valid, false otherwise
- return (bool) preg_match($regex, $phonenumber);
+ return (bool) preg_match(PHONE_ES_REGEX, $phonenumber);
}
/**
@@ -48,26 +37,17 @@ function valid_es_phone_number($phonenumber) {
*
*/
function format_es_phone_number($phonenumber, $field = FALSE) {
-
- // define regular expression
- //$regex = "/
- // \D* # optional separator
- // ([69]\d{2}) # first group of numbers
- // \D* # optional separator
- // (\d{3}) # second group
- // \D* # optional separator
- // (\d{3}) # third group
- // \D* # ignore trailing non-digits
- // $/x";
-
- $regex = '/^[0-9]{2,3}-? ?[0-9]{6,7}$/';
-
+ $phone = str_replace(array(' ', '-', '(', ')'), '', $phonenumber);
// get digits of phone number
- preg_match($regex, $phonenumber, $matches);
+ preg_match(PHONE_ES_REGEX, $phone, $matches);
- // construct ten-digit phone number
- $phonenumber = $matches[1] . ' ' . $matches[2] . ' ' . $matches[3];
+ $formatedphone = '';
+ if ($field && $field['phone_country_code']) {
+ $formatedphone .= '+34 ';
+ }
+ $formatedphone .= $matches[3];
+ $formatedphone .= ' ' . $matches[4] . ' ';
+ $formatedphone .= '' . $matches[5];
- return $phonenumber;
+ return $formatedphone;
}
-