phone.ru.inc in Phone 5
Same filename and directory in other branches
File
phone.ru.incView source
<?php
/**
* Verifies that $phonenumber is a valid ten-digit Russian phone number
*
* @param string $phonenumber
* @return boolean Returns boolean FALSE if the phone number is not valid.
*/
function valid_ru_phone_number($phonenumber) {
//$phonenumber = trim($phonenumber);
// define regular expression
$regex = "/\n \\D* # ignore non-digits\n [78]? # an optional 78\n \\D* # optional separator \n \\d{3,5} # area code 3-5 digit\n \\D* # optional separator\n \\d{1,3} # 3-digit prefix\n \\D* # optional separator\n \\d{2} # 2-digit line number\n \\D* # optional separator\n \\d{2} # 2-digit line number\n \\D* # ignore trailing non-digits\n /x";
// return true if valid, false otherwise
return (bool) preg_match($regex, $phonenumber);
}
/**
* Convert a valid Russian phone number into standard +7 (495) 567-53-09 or +7 (444xx) 67-53-09 or mobile 8 910 414-56-90 format
*
* @param $phonenumber must be a valid ten-digit number (with optional extension)
*
*/
function format_ru_phone_number($phonenumber) {
// define regular expression
$regex = "/\n ^\\D* # ignore non-digits\n ([78])? # an optional 78\n \\D* # optional separator \n (\\d{3,5}) \t # area code 3-5 digit\n \\D* # optional separator\n (\\d{1,3}) \t # capture 3-digit prefix\n \\D* # optional separator\n (\\d{2}) # 2-digit line number\n \\D* # optional separator\n (\\d{2}) # 2-digit line number\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] . ' - ' . $matches[4] . ' - ' . $matches[5];
return $phonenumber;
}
Functions
Name | Description |
---|---|
format_ru_phone_number | Convert a valid Russian phone number into standard +7 (495) 567-53-09 or +7 (444xx) 67-53-09 or mobile 8 910 414-56-90 format |
valid_ru_phone_number | Verifies that $phonenumber is a valid ten-digit Russian phone number |