Getting Started
Phone Number is a PHP library for parsing, validating, and formatting phone numbers using libphonenumber.
Installation
Section titled “Installation”composer require cline/phone-numberBasic Usage
Section titled “Basic Usage”use Cline\PhoneNumber\PhoneNumber;
// Parse a phone number$phone = PhoneNumber::parse('+1 (555) 123-4567');
// Get formatted versions$phone->format(); // "+15551234567"$phone->formatNational(); // "(555) 123-4567"$phone->formatInternational(); // "+1 555-123-4567"$phone->formatE164(); // "+15551234567"
// Get components$phone->getCountryCode(); // 1$phone->getNationalNumber(); // "5551234567"$phone->getRegionCode(); // "US"Validation
Section titled “Validation”use Cline\PhoneNumber\PhoneNumber;
// Check if valid$phone = PhoneNumber::parse('+1 555 123 4567');$phone->isValid(); // true
// Invalid number$phone = PhoneNumber::parse('not a number');$phone->isValid(); // false
// Validate for specific region$phone->isValidForRegion('US'); // true$phone->isValidForRegion('GB'); // falseParsing with Region
Section titled “Parsing with Region”// Parse with default region$phone = PhoneNumber::parse('555-123-4567', 'US');
// Useful for numbers without country code$phone = PhoneNumber::parse('020 7946 0958', 'GB');$phone->formatInternational(); // "+44 20 7946 0958"Phone Number Types
Section titled “Phone Number Types”$phone = PhoneNumber::parse('+1 555 123 4567');
$phone->getType(); // "FIXED_LINE_OR_MOBILE"$phone->isMobile(); // true/false$phone->isFixedLine(); // true/false$phone->isTollFree(); // falseNext Steps
Section titled “Next Steps”- Basic Usage - Detailed parsing and formatting
- Exception Handling - Handle parsing errors
- Metadata - Access phone number metadata