问题
I am using https://github.com/davideme/libphonenumber-for-PHP for formatting phonenumbers
I am having issue with 1800 numbers
$formatnumber = "1800123456"
$country = "AU";
$phoneUtil = PhoneNumberUtil::getInstance();
try {
$NumberProto = $phoneUtil->parse( $formatnumber , $country );
} catch ( NumberParseException $e ) {
echo $e;
}
$formattedNumber = $phoneUtil->format( $NumberProto, PhoneNumberFormat::NATIONAL );
What I am expecting to get back from $formattedNumber
is "1800 123456"
What I get back is an unformatted number "1800123456"
If there something I need to do doing to get it to format correctly?
回答1:
It works for me with pretty much your code:
<?php
require __DIR__ . '/vendor/autoload.php';
$formatnumber = "1800123456";
$country = "AU";
$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
try {
$NumberProto = $phoneUtil->parse($formatnumber, $country);
} catch (NumberParseException $e) {
echo $e;
}
$formattedNumber = $phoneUtil->format($NumberProto, \libphonenumber\PhoneNumberFormat::NATIONAL);
echo $formattedNumber;
Response:
1800 123 456
I am using my version of libphonenumber-for-php though: https://github.com/giggsey/libphonenumber-for-php
来源:https://stackoverflow.com/questions/15125433/libphonenumber-for-php