function phone_libphonenumber_format in Phone 7.2
Formats a phone number using libphonenumber.
Parameters
string $number: The raw phone number we are working with.
string $countrycode: The selected countrycode for this number.
string $extension: An extension number.
string $format: The format to return the number in. Can be one of: o phone_national o phone_e164 o phone_rfc3966 o phone_international Defaults to phone_international when an unrecognised format is provided. NB: If $allow_alpha is TRUE, and the number contains alpha characters, then the number will be returned as it was entered, and will ignore this format option.
bool $allow_alpha: When set to TRUE, and $number contains alpha characters, i.e. is a vanity number, then retain the alpha formatting, and do not convert the number to its numeric equivalent.
bool $extension_prefix: When set, allows adjusting the extension prefix. This option is ignored when $format is phone_rfc3966.
Return value
string Return the formatted number, or FALSE on error.
2 calls to phone_libphonenumber_format()
- phone_field_formatter_view in ./
phone.module - Implements hook_field_formatter_view().
- phone_tokens in ./
phone.tokens.inc - Implements hook_tokens().
File
- includes/
phone.libphonenumber.inc, line 181 - Provides integration functions with the libraries API and libphonenumber.
Code
function phone_libphonenumber_format($number, $countrycode, $extension, $format, $allow_alpha = FALSE, $extension_prefix = '') {
try {
// Get the parsed phone objects.
list($phoneutil, $phonenumber) = _phone_libphonenumber($number, $countrycode, $extension, $allow_alpha);
} catch (libphonenumber\NumberParseException $e) {
// Uh oh... What can we do?
return FALSE;
}
// If we allow alpha characters, and have some, then output the number as is.
if ($allow_alpha && $phoneutil
->isAlphaNumber($number)) {
// We put this through check plain for extra safety. We could probably get
// away without it, unless libphonenumber was installed after data had
// already been entered.
return check_plain($phoneutil
->formatOutOfCountryKeepingAlphaChars($phonenumber, $countrycode));
}
$add_extension = '';
if ($format != 'phone_rfc3966' && !empty($extension_prefix) && $extension_prefix != libphonenumber\PhoneNumberUtil::DEFAULT_EXTN_PREFIX && $phonenumber
->hasExtension()) {
$add_extension = check_plain($extension_prefix) . $phonenumber
->getExtension();
$phonenumber
->clearExtension();
}
// Get the right libphonenumber format option.
switch ($format) {
case 'phone_national':
$format = libphonenumber\PhoneNumberFormat::NATIONAL;
break;
case 'phone_e164':
$format = libphonenumber\PhoneNumberFormat::E164;
break;
case 'phone_rfc3966':
$format = libphonenumber\PhoneNumberFormat::RFC3966;
break;
case 'phone_international':
default:
$format = libphonenumber\PhoneNumberFormat::INTERNATIONAL;
break;
}
// As above, we probably don't need to put this though check plain,
// but short of checking all the possible output options from libphonenumber.
return check_plain($phoneutil
->format($phonenumber, $format)) . $add_extension;
}