function phone_libphonenumber_clean in Phone 7.2
Cleans up phone number components for saving into the database.
Parameters
string $number: The raw phone number we are working with.
sring $countrycode: The selected countrycode for this number.
string $extension: An extension number.
Return value
array An array with the number, countrycode, and extension properties, or an empty array if there is an error.
1 call to phone_libphonenumber_clean()
- phone_field_presave in ./
phone.module - Implements hook_field_presave().
File
- includes/
phone.libphonenumber.inc, line 244 - Provides integration functions with the libraries API and libphonenumber.
Code
function phone_libphonenumber_clean($number, $countrycode, $extension) {
try {
// Get the parsed phone objects, preserving the alpha characers.
// Also, don't initialise the extension, otherwise it will be included in
// the output below, which is wrong.
list($phoneutil, $phonenumber) = _phone_libphonenumber($number, $countrycode, '', TRUE);
// Get the actual countrycode used, in case we didn't have one before,
// and libphonenumber was able to detect one from a country code.
if ($phonenumber
->getCountryCodeSource() != libphonenumber\CountryCodeSource::FROM_DEFAULT_COUNTRY) {
$new_countrycode = $phoneutil
->getRegionCodeForCountryCode($phonenumber
->getCountryCode());
// If the country code is empty, then set the new one,
// if the country code is different, lets see if they have the same
// calling code, if they do, use our original country code.
// i.e. Calling Code of +1 is always the US, but the user might have
// input Barbados for example.
if (empty($countrycode) || $countrycode != $new_countrycode && $phonenumber
->getCountryCode() != $phoneutil
->getMetadataForRegion($countrycode)
->getCountryCode()) {
$countrycode = $new_countrycode;
}
}
// Extensions should only ever be digits.
$force_non_alpha = FALSE;
if ($phonenumber
->hasExtension()) {
$extension = preg_replace('/[^0-9]/', '', $phonenumber
->getExtension());
$phonenumber
->clearExtension();
$force_non_alpha = TRUE;
}
else {
$extension = '';
}
// Numbers that have alpha characters are returned as is.
// Otherwise store the national formatted number with all spaces stripped.
if ($force_non_alpha || !$phoneutil
->isAlphaNumber($number)) {
$number = $phonenumber
->getNationalNumber();
}
return array(
'countrycode' => $countrycode,
'number' => $number,
'extension' => $extension,
);
} catch (libphonenumber\NumberParseException $e) {
// Uh oh. Do nothing in effect.
return array();
}
}