diff --git a/include/phone.es.inc b/include/phone.es.inc index c4a05ad..723dd97 100644 --- a/include/phone.es.inc +++ b/include/phone.es.inc @@ -25,16 +25,16 @@ function valid_es_phone_number($phonenumber) { // define regular expression - $regex = '/ - \D* # optional separator - (\+[0-9]{2})? # optional international code - \D* # optional separator - \d{3} # first number group - \D* # optional separator - \d{3} # second number group - \D* # optional separator - \d{3} # third number group - $/x'; + $regex = '/ + \D* # optional separator + (\+[0-9]{2})? # optional international code + \D* # optional separator + \d{3} # first number group + \D* # optional separator + \d{3} # second number group + \D* # optional separator + \d{3} # third number group + $/x'; // return true if valid, false otherwise @@ -50,24 +50,27 @@ function valid_es_phone_number($phonenumber) { function format_es_phone_number($phonenumber, $field = FALSE) { // define regular expression - $regex = '/ - \D* # optional separator - (\+[0-9]{2})? # optional international code - \D* # optional separator - (\d{3}) # first number group - \D* # optional separator - (\d{3}) # second number group - \D* # optional separator - (\d{3}) # third number group - $/x'; + $regex = '/ + \D* # optional separator + (\+[0-9]{2})? # optional international code + \D* # optional separator + (\d{3}) # first number group + \D* # optional separator + (\d{3}) # second number group + \D* # optional separator + (\d{3}) # third number group + $/x'; // get digits of phone number preg_match($regex, $phonenumber, $matches); // construct ten-digit phone number - - $phonenumber = $matches[1] . ' ' . $matches[2] . ' ' . $matches[3] . ' ' . $matches[4] ; + if ($matches[1] > 0) { + $phonenumber = $matches[1] . ' ' . $matches[2] . ' ' . $matches[3] . ' ' . $matches[4] ; + } + else { + $phonenumber = $matches[2] . ' ' . $matches[3] . ' ' . $matches[4]; + } return $phonenumber; -} - +} \ No newline at end of file