--- phone.es.inc.orig 2011-01-03 15:40:32.000000000 +0100 +++ phone.es.inc 2014-01-27 15:55:10.203912230 +0100 @@ -5,12 +5,18 @@ * @file * CCK Field for Spanish phone numbers. */ + +/** + * 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( 'label' => 'Phone Numbers - Spain', - 'error' => '"%value" is not a valid Spanish phone number
Spanish phone numbers should only contains numbers and spaces and be like 999 999 999', + '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', ); } @@ -22,25 +28,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); } /** @@ -50,26 +40,17 @@ function valid_es_phone_number($phonenum * */ 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; } -