简体   繁体   中英

Validate number as a UK mobile number only using libphonenumber-for-php

I am using libphonenumber-for-php to write a validation class that will check if a number is a UK mobile number. Anything else will fail.

public function validate(){
    $phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
    $carrierMapper = \libphonenumber\PhoneNumberToCarrierMapper::getInstance();

    foreach ($this->phoneNumbers as $number){
        try {
            $phoneNumberObject = $phoneUtil->parse($number, 'GB');
        }catch (NumberParseException $e){
            echo $e;
        }

        $valid = $phoneUtil->isValidNumber($phoneNumberObject);
        $type = $phoneUtil->getNumberType($phoneNumberObject); 
        $carrier = $carrierMapper->getNameForNumber($phoneNumberObject, 'en');
    }
}

Is this all that is required? I check if it is a valid number then check if it is of the mobile number type. Is there anything else I should do to be 100% certain the passed in number is a Valid UK mobile number?

I think the method for getting a carrier is getDescriptionForNumber. I am not a complete PHP programmer, but I use this library in Python and C and then I got the same error because of that method.

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