diff --git a/phone.fr.inc b/phone.fr.inc index 8f06e4e..2e2b937 100644 --- a/phone.fr.inc +++ b/phone.fr.inc @@ -5,13 +5,14 @@ * CCK Field for French phone numbers. */ -define('PHONE_FR_REGEX', '/(\+33|0)([1-9]\d{8}|85\d{7}|87[0-57-9]\d{6})$/'); +define('PHONE_FR_REGEX', '/^((?:0(?:0|11)|\+)33(?:0)?|0)([1-9]\d{8}(#\d{3,4})?)$/'); +define('PHONE_FR_REGEX_NSN', '/^(([1-79])|(8\d{2}))(\d{2})(\d{2})(\d{2})(\d{2})?(#\d{3,4})?$/'); function phone_fr_metadata() { // These strings are translated using t() on output. return array( 'label' => 'Phone Numbers - France', - 'error' => '"%value" is not a valid French phone number
French phone numbers should only contain numbers and spaces and be like 99 99 99 99 99', + 'error' => '"%value" is not a valid French phone number
French phone numbers should only contain numbers and spaces and be like 09 99 99 99 99', ); } @@ -25,7 +26,7 @@ function phone_fr_metadata() { */ function valid_fr_phone_number($phonenumber) { - //$phonenumber = trim($phonenumber); + $phonenumber = trim($phonenumber); $phonenumber = str_replace(array(' ','-','(',')'), '', $phonenumber); return (bool) preg_match(PHONE_FR_REGEX, $phonenumber); @@ -37,11 +38,32 @@ function valid_fr_phone_number($phonenumber) { * @param string $phonenumber * @return string Returns a string containting the phone number with some formatting. */ + function format_fr_phone_number($phonenumber, $field = FALSE) { - $phone = str_replace(array(' ','-','(',')'), '', $phonenumber); - if (preg_match(PHONE_FR_REGEX, $phone, $matches) != 1) { + + $phonenumber = trim($phonenumber); + + $phone = str_replace(array(" ","-","(",")"), "", $phonenumber); + + if (!preg_match(PHONE_FR_REGEX, $phone, $matches)) { return $phonenumber; // not a french phone number + } else { + $phoneprefix = $matches['1']; + $phonensn = $matches['2']; + + if (preg_match(PHONE_FR_REGEX_NSN, $phonensn, $matchesnsn)) { + $phonensn = trim($matchesnsn['1'] . " " . $matchesnsn['4'] . " " . $matchesnsn['5'] . " " . $matchesnsn['6'] . " " . $matchesnsn['7'] . " " . $matchesnsn['8']); + if ($field['phone_country_code']) { + $phonenumber = "+33 " . " " . $phonensn; + } else { + if ($phoneprefix == "0") { + $phonenumber = "0" . $phonensn; + } else { + $phonenumber = "+33" . " " . $phonensn; + } + } + } } - return ($field && $field['phone_country_code'] ? '+33 ' : '0') . $matches[2]; + return $phonenumber; }