Parsing is the process of converting a phone number string into a structured PhoneNumber object. This is the foundation of all phone number operations in libphonenumber.
Region codes are CLDR two-letter codes (e.g., GB, US, FR). They help the parser understand which country’s numbering plan to use.
1
International Format
When the number starts with +, the region code is optional:
// Region code can be null for international format$number = $phoneUtil->parse('+41 44 668 18 00', null);
2
National Format
For national format numbers, you must provide the region code:
// Region code required for national format$number = $phoneUtil->parse('044 668 18 00', 'CH');
3
International Dialing Prefix
Numbers with IDD prefix (like 00 or 011) need the calling region:
// Parse from France (uses 00 as IDD)$number = $phoneUtil->parse('00 44 117 496 0123', 'FR');// Parse from US (uses 011 as IDD)$number = $phoneUtil->parse('011 44 117 496 0123', 'US');
try { $number = $phoneUtil->parse($input, $region); // Additional validation recommended if (!$phoneUtil->isValidNumber($number)) { throw new InvalidArgumentException('Number is not valid'); }} catch (NumberParseException $e) { // Handle specific error types switch ($e->getErrorType()) { case NumberParseException::INVALID_COUNTRY_CODE: echo "Invalid country code"; break; case NumberParseException::NOT_A_NUMBER: echo "Not a valid phone number"; break; case NumberParseException::TOO_SHORT_NSN: echo "Number is too short"; break; case NumberParseException::TOO_LONG: echo "Number is too long"; break; }}
Parsing success doesn’t guarantee the number is valid! Always validate after parsing for production use.