NumberParseException is thrown when the library encounters an error while parsing a phone number string. This exception provides detailed information about what went wrong through error types and messages.
try { $phoneUtil->parse('invalid', 'US');} catch (\libphonenumber\NumberParseException $e) { echo $e->getMessage(); // "The string supplied did not seem to be a phone number"}
use libphonenumber\NumberParseException;try { $number = $phoneUtil->parse($input, $region);} catch (NumberParseException $e) { $message = match ($e->getErrorType()) { NumberParseException::INVALID_COUNTRY_CODE => "Invalid country code. Please check the country.", NumberParseException::NOT_A_NUMBER => "This doesn't look like a phone number.", NumberParseException::TOO_SHORT_AFTER_IDD => "Number too short after international prefix.", NumberParseException::TOO_SHORT_NSN => "Phone number is too short.", NumberParseException::TOO_LONG => "Phone number is too long.", default => "Invalid phone number: " . $e->getMessage(), }; echo $message;}
class PhoneNumberFormValidator{ private array $errors = []; public function validatePhoneNumber(string $input, string $region): bool { $phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance(); // Clear previous errors $this->errors = []; // Check if empty if (trim($input) === '') { $this->errors[] = 'Phone number is required'; return false; } try { $number = $phoneUtil->parse($input, $region); // Additional validation: check if valid if (!$phoneUtil->isValidNumber($number)) { $this->errors[] = 'This phone number is not valid for ' . $region; return false; } return true; } catch (\libphonenumber\NumberParseException $e) { $this->errors[] = $this->getErrorMessage($e, $region); return false; } } private function getErrorMessage( \libphonenumber\NumberParseException $e, string $region ): string { return match ($e->getErrorType()) { \libphonenumber\NumberParseException::INVALID_COUNTRY_CODE => 'Invalid country code', \libphonenumber\NumberParseException::NOT_A_NUMBER => 'Please enter a valid phone number', \libphonenumber\NumberParseException::TOO_SHORT_NSN => "Phone number is too short for {$region}", \libphonenumber\NumberParseException::TOO_LONG => "Phone number is too long for {$region}", default => 'Invalid phone number format', }; } public function getErrors(): array { return $this->errors; }}// Usage in a form handler$validator = new PhoneNumberFormValidator();if ($validator->validatePhoneNumber($_POST['phone'], 'US')) { // Process the valid phone number echo "Phone number is valid!";} else { // Display errors foreach ($validator->getErrors() as $error) { echo "<p class='error'>{$error}</p>"; }}
try { // Less than 3 digits $number = $phoneUtil->parse('12', 'US');} catch (\libphonenumber\NumberParseException $e) { // Error type: NOT_A_NUMBER (1) echo $e->getMessage(); // "The string supplied did not seem to be a phone number"}
try { // International prefix but incomplete number $number = $phoneUtil->parse('+1 2', 'US');} catch (\libphonenumber\NumberParseException $e) { // Error type: TOO_SHORT_AFTER_IDD (2) echo $e->getMessage(); // "Phone number too short after IDD"}
try { // Valid country code but national number too short $number = $phoneUtil->parse('123', 'US');} catch (\libphonenumber\NumberParseException $e) { // Error type: TOO_SHORT_NSN (3) echo $e->getMessage(); // "The string supplied is too short to be a phone number"}
try { // Way too many digits $number = $phoneUtil->parse('12345678901234567890', 'US');} catch (\libphonenumber\NumberParseException $e) { // Error type: TOO_LONG (4) echo $e->getMessage(); // "The string supplied is too long to be a phone number"}