The getInstance() method returns a singleton, so you can call it multiple times without performance concerns.
2
Parse a Phone Number
Parse a phone number string into a PhoneNumber object. You need to provide the number and a region code:
try { // Parse with region code $swissNumber = $phoneUtil->parse("044 668 18 00", "CH"); // Parse international format (region code can be null) $ukNumber = $phoneUtil->parse("+44 117 496 0123", null); // Parse with different region $usNumber = $phoneUtil->parse("+1 650 253 0000", "US");} catch (NumberParseException $e) { echo "Error parsing number: " . $e->getMessage();}
If the number is in international format (starts with +), the region code can be null. Otherwise, the region code helps determine the correct country.
3
Validate the Number
Check if the parsed phone number is valid:
// Check if number is valid$isValid = $phoneUtil->isValidNumber($swissNumber);if ($isValid) { echo "The number is valid!\n";} else { echo "The number is not valid.\n";}// Check if number is possible (lighter validation)$isPossible = $phoneUtil->isPossibleNumber($swissNumber);// Check if valid for specific region$isValidForRegion = $phoneUtil->isValidNumberForRegion($ukNumber, "GB");
isValidNumber() validates number patterns but cannot check if a number is actually in use. It only verifies that the number follows the rules for that country.
4
Format the Number
Format the phone number in different styles:
// E.164 format (international standard)echo $phoneUtil->format($swissNumber, PhoneNumberFormat::E164);// Output: +41446681800// International format (human-readable)echo $phoneUtil->format($swissNumber, PhoneNumberFormat::INTERNATIONAL);// Output: +41 44 668 18 00// National format (as dialed within the country)echo $phoneUtil->format($swissNumber, PhoneNumberFormat::NATIONAL);// Output: 044 668 18 00// RFC3966 format (for tel: URIs)echo $phoneUtil->format($swissNumber, PhoneNumberFormat::RFC3966);// Output: tel:+41-44-668-18-00
5
Format for International Dialing
Format a number as it would be dialed from another country:
// How to dial from the United Statesecho $phoneUtil->formatOutOfCountryCallingNumber($swissNumber, "US");// Output: 011 41 44 668 18 00// How to dial from Great Britainecho $phoneUtil->formatOutOfCountryCallingNumber($swissNumber, "GB");// Output: 00 41 44 668 18 00// Dialing from within Switzerlandecho $phoneUtil->formatOutOfCountryCallingNumber($swissNumber, "CH");// Output: 044 668 18 00