I'm using this PHP port of libphonenumber because the other one requires mbstring which my version of PHP doesn't have installed. https://github.com/davideme/libphonenumber-for-PHP
Now, it doesn't matter what I pass in the $country
value below. I always get the exception:
Missing or invalid default region
What on earth am I doing wrong here?
I am porting some javascript code to use the PHP libraries in an ExpressionEngine plugin.
Here's the PHP code:
function getNumberParts($country, $phone) {
try {
$cleanphone = $this->cleanPhone($phone);
$phoneUtil = PhoneNumberUtil::getInstance();
$region = $phoneUtil->getRegionCodeForCountryCode($country);
$number = $phoneUtil->parse($cleanphone, $region);
$e164 = $phoneUtil->format($number, 0);
$num = $phoneUtil->parseAndKeepRawInput($e164, "ZZ");
$ctry = $phoneUtil->getRegionCodeForNumber($num);
$NSN = $phoneUtil->getNationalSignificantNumber($num);
$intl = str_replace("tel:+", "", $phoneUtil->format($num, 3));
$output = array("Country"=>"", "CountryCode"=>"", "AreaCode"=>"", "Number"=>"", "Intl"=>"", "FullNumber"=>"");
$areaCodeLen = $phoneUtil->getLengthOfNationalDestinationCode($num);
$areaCode = '';
if ($areaCodeLen > 0) {
$subscriberNumber = substr($NSN, $areaCodeLen);
$areaCode = substr($NSN, 0, $areaCodeLen);
} else {
$subscriberNumber = $NSN;
}
$output["Country"] = $ctry;
$output["CountryCode"] = $cc;
$output["AreaCode"] = $areaCode;
$output["Number"] = $subscriberNumber;
$output["Intl"] = $intl;
$output["FullNumber"] = $output["CountryCode"] . '-' + $output["AreaCode"] . '-' + $output["Number"];
return $output;
} catch (NumberParseException $e) {
$this->formatNumber($phone);
}
}
function formatNumber($a, $b) {
try {
$b = cleanPhone($b);
$c = PhoneNumberUtil::getInstance();
$e = $c->parseAndKeepRawInput($b, $a)
$f = $c->format($e, PhoneNumberFormat::RFC3966);
return json_encode($f);
} catch (NumberParseException $e) {
return $b
}
}
This is the WORKING javascript function I'm trying to convert that utilizes the javascript port of libphonenumber:
function getNumberParts(country, phone) {
try {
phone = cleanPhone(phone);
var phoneUtil = i18n.phonenumbers.PhoneNumberUtil.getInstance();
var cc = phoneUtil.getCountryCodeForRegion(country);
var number = phoneUtil.parseAndKeepRawInput(phone, country)
var e164 = phoneUtil.format(number, 0);
var num = phoneUtil.parseAndKeepRawInput(e164, "ZZ");
var ctry = phoneUtil.getRegionCodeForNumber(num);
var NSN = phoneUtil.getNationalSignificantNumber(num);
intl = phoneUtil.format(num, 3)
intl = intl.replace("tel:+", "");
var output = { "Country": "", "CountryCode": "", "AreaCode": "", "Number": "", "Intl":"", "FullNumber": "" };
var areaCodeLen = phoneUtil.getLengthOfNationalDestinationCode(num);
var areaCode = '';
intlNumber = "";
if (areaCodeLen > 0) {
subscriberNumber = NSN.substring(areaCodeLen);
areaCode = NSN.substring(0, areaCodeLen);
} else {
subscriberNumber = NSN;
}
output.Country = ctry;
output.CountryCode = number.getCountryCode();
output.AreaCode = areaCode;
output.Number = subscriberNumber;
output.Intl = intl;
output.FullNumber = number.getCountryCode() + '-' + areaCode + '-' + subscriberNumber;
return output;
} catch (l) {
return formatNumber(country, phone);
}
}
Even using the example code throws the same exception:
$phoneUtil = PhoneNumberUtil::getInstance();
try {
$NumberProto = $phoneUtil->parse($cleanphone, "US");
var_dump($NumberProto);
} catch (NumberParseException $e) {
echo $e;
}
What IS very strange is that US AND ZZ are NOT in the supportedRegions array.
I found out what is removing it from the supportedRegions... This call:
unset($this->supportedRegions[array_search(self::REGION_CODE_FOR_NON_GEO_ENTITY, $this->supportedRegions)])
Not sure why this is being done. The REGION_CODE_FOR_NON_GEO_ENTITY
is set to '001' so it is removing the US entry from the supportedRegions array.
I finally fixed it by taking the code from the init
function in the other PHP port and putting it in this one. I didn't notice but this one also used mbstring function mb_substr but I don't need it and modified it to just use substr.
All is working now.