function hu_parts in Phone Number 7
Verifies and mage parts from $number
Parameters
$number: Digits only value.
Return value
boolean Array if array['error'] is present, the validation fails else ['area'] for area number ['first'] ['second'] for the AA/BBB-CCC numbers ['first'] ['second'] ['third'] for the A(A)/BBB-CC-DD numbers
3 calls to hu_parts()
- hu_formatter_default in includes/
phone.hu.inc - Default formatter for international phone number.
- hu_formatter_local in includes/
phone.hu.inc - Local formatter for local phone number.
- hu_validate_number in includes/
phone.hu.inc - Verifies that $number is a valid nine digit Hungarian phone number.
File
- includes/
phone.hu.inc, line 20 - CCK Field for Hungarian phone numbers.
Code
function hu_parts($number) {
/**
* Because Hungary an telephone numbers are both in open and closed number region, the area code length may be vary. If the area code is 1, 7 more digits follow it.
* If the area code is 20/30/70, also 7 more digits.
* If any other 2 digits are code is appear, we want 6 more digits.
*
* So complecated, I know :)
*
*/
$result = array();
// Last 9 digits
$n = drupal_substr($number, -9);
if ($n === FALSE || drupal_substr($n, 0, 1) != 2 && drupal_substr($n, 0, 1) != 3 && drupal_substr($n, 0, 1) != 7) {
// No 9 digit, check for 8 digit
if (drupal_substr($number, -8) === FALSE) {
// No 8 digit, too short for valid telephone number
$result['error'] = '"%phone_input" is not a valid Hungarian phone number, it should be a 9 digit number like "20/123-45-67" or a 8 digit number like "23/123-456"';
}
else {
$n = drupal_substr($number, -8);
// It is 8 digits
if (drupal_substr($n, 0, 1) == 1) {
// A number from Budapest
$result['area'] = drupal_substr($n, 0, 1);
$result['first'] = drupal_substr($n, 1, 3);
$result['second'] = drupal_substr($n, 4, 2);
$result['third'] = drupal_substr($n, 6, 2);
}
else {
// A number from countryside
$result['area'] = drupal_substr($n, 0, 2);
$result['first'] = drupal_substr($n, 2, 3);
$result['second'] = drupal_substr($n, 5, 3);
}
}
}
else {
// Cell phone number
$result['area'] = drupal_substr($n, 0, 2);
$result['first'] = drupal_substr($n, 2, 3);
$result['second'] = drupal_substr($n, 5, 2);
$result['third'] = drupal_substr($n, 7, 2);
}
return $result;
}