简体   繁体   中英

libphonenumber-for-PHP fails to validate phone number

Iam using https://github.com/davideme/libphonenumber-for-PHP for international phone no validation.It's working fine,but as per the guidence in that github page i have given

"$swissNumberStr = "044 668 18 00";" and $phoneUtil->parse($swissNumberStr, "CH") ;

as inputs... and when we are calling

$isValid = $phoneUtil->isValidNumber($swissNumberProto);

it should return true..because its a valid one.But for me it's getting false..any help would be much appreciated..

demo.php

use com\google\i18n\phonenumbers\PhoneNumberUtil;
use com\google\i18n\phonenumbers\PhoneNumberFormat;
use com\google\i18n\phonenumbers\NumberParseException;

require_once 'PhoneNumberUtil.php';

$swissNumberStr = "044 668 18 00";
$phoneUtil = PhoneNumberUtil::getInstance();
try {
    $swissNumberProto = $phoneUtil->parseAndKeepRawInput($swissNumberStr, "CH");
    echo $phoneUtil->getNumberType($swissNumberProto);
    //var_dump($swissNumberProto);
} catch (NumberParseException $e) {
    echo $e;
}
$isValid = $phoneUtil->isValidNumber($swissNumberProto);//return true
var_dump($isValid);
// Produces "+41446681800"
echo $phoneUtil->format($swissNumberProto, PhoneNumberFormat::INTERNATIONAL) . PHP_EOL;
echo $phoneUtil->format($swissNumberProto, PhoneNumberFormat::NATIONAL) . PHP_EOL;
echo $phoneUtil->format($swissNumberProto, PhoneNumberFormat::E164) . PHP_EOL;

echo $phoneUtil->formatOutOfCountryCallingNumber($swissNumberProto, "US") . PHP_EOL;

You are using the wrong function for parsing.

Use

phUtil.Parse(germanNumberStr, "CH")

instead of parseAndKeepRawInput

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM