libphonenumber\PhoneNumberUtil::parse PHP Method

parse() public method

It will accept a number in any format (E164, national, international etc), assuming it can interpreted with the defaultRegion supplied. It also attempts to convert any alpha characters into digits if it thinks this is a vanity number of the type "1800 MICROSOFT".

This method will throw a {@link NumberParseException} if the number is not considered to be a possible number. Note that validation of whether the number is actually a valid number for a particular region is not performed. This can be done separately with {@link #isValidnumber}.

public parse ( string $numberToParse, string $defaultRegion, PhoneNumber $phoneNumber = null, boolean $keepRawInput = false ) : PhoneNumber
$numberToParse string number that we are attempting to parse. This can contain formatting such as +, ( and -, as well as a phone number extension.
$defaultRegion string region that we are expecting the number to be from. This is only used if the number being parsed is not written in international format. The country_code for the number in this case would be stored as that of the default region supplied. If the number is guaranteed to start with a '+' followed by the country calling code, then "ZZ" or null can be supplied.
$phoneNumber PhoneNumber
$keepRawInput boolean
return PhoneNumber a phone number proto buffer filled with the parsed number
    public function parse($numberToParse, $defaultRegion, PhoneNumber $phoneNumber = null, $keepRawInput = false)
    {
        if ($phoneNumber === null) {
            $phoneNumber = new PhoneNumber();
        }
        $this->parseHelper($numberToParse, $defaultRegion, $keepRawInput, true, $phoneNumber);
        return $phoneNumber;
    }

Usage Example

Example #1
1
 public function testChineseCarrierLookup()
 {
     $number = $this->phoneUtil->parse("+86 150 3657 7264", "CN");
     $carrier = PhoneNumberToCarrierMapper::getInstance();
     $location = $carrier->getNameForNumber($number, "en");
     $this->assertEquals("China Mobile", $location);
 }
All Usage Examples Of libphonenumber\PhoneNumberUtil::parse
PhoneNumberUtil