giggsey / libphonenumber-for-php
A library for parsing, formatting, storing and validating international phone numbers, a PHP Port of Google's libphonenumber.
Installs: 118 735 440
Dependents: 423
Suggesters: 32
Security: 0
Stars: 4 844
Watchers: 108
Forks: 469
Open Issues: 1
Requires
- php: ^8.1
- giggsey/locale: ^2.7
- symfony/polyfill-mbstring: ^1.31
Requires (Dev)
- ext-dom: *
- friendsofphp/php-cs-fixer: ^3.71
- infection/infection: ^0.28.0
- nette/php-generator: ^4.1
- php-coveralls/php-coveralls: ^2.7
- phpstan/extension-installer: ^1.4.3
- phpstan/phpstan: ^2.1.7
- phpstan/phpstan-deprecation-rules: ^2.0.1
- phpstan/phpstan-phpunit: ^2.0.4
- phpstan/phpstan-strict-rules: ^2.0.3
- phpunit/phpunit: ^10.5.45
- symfony/console: ^6.4
- symfony/filesystem: ^6.4
- symfony/process: ^6.4
Replaces
- dev-master / 9.x-dev
- 9.0.2
- 9.0.1
- 9.0.0
- 8.13.55
- 8.13.54
- 8.13.53
- 8.13.52
- 8.13.51
- 8.13.50
- 8.13.49
- 8.13.48
- 8.13.47
- 8.13.46
- 8.13.45
- 8.13.44
- 8.13.43
- 8.13.42
- 8.13.41
- 8.13.40
- 8.13.39
- 8.13.38
- 8.13.37
- 8.13.36
- 8.13.35
- 8.13.34
- 8.13.33
- 8.13.32
- 8.13.31
- 8.13.30
- 8.13.29
- 8.13.28
- 8.13.27
- 8.13.26
- 8.13.25
- 8.13.24
- 8.13.23
- 8.13.22
- 8.13.21
- 8.13.20
- 8.13.19
- 8.13.18
- 8.13.17
- 8.13.16
- 8.13.15
- 8.13.14
- 8.13.13
- 8.13.12
- 8.13.11
- 8.13.10
- 8.13.9
- 8.13.8
- 8.13.7
- 8.13.6
- 8.13.5
- 8.13.4
- 8.13.3
- 8.13.2
- 8.13.1
- 8.13.0
- 8.12.57
- 8.12.56
- 8.12.55
- 8.12.54
- 8.12.53
- 8.12.52
- 8.12.51
- 8.12.50
- 8.12.49
- 8.12.48
- 8.12.47.1
- 8.12.47
- 8.12.46
- 8.12.45
- 8.12.44
- 8.12.43
- 8.12.42
- 8.12.41
- 8.12.40
- 8.12.39
- 8.12.38
- 8.12.37
- 8.12.36
- 8.12.35
- 8.12.34
- 8.12.33
- 8.12.32
- 8.12.31
- 8.12.30
- 8.12.29
- 8.12.28
- 8.12.27
- 8.12.26
- 8.12.25
- 8.12.24
- 8.12.23
- 8.12.22
- 8.12.21
- 8.12.20
- 8.12.19
- 8.12.18
- 8.12.17
- 8.12.16
- 8.12.15
- 8.12.14
- 8.12.13
- 8.12.12
- 8.12.11
- 8.12.10
- 8.12.9
- 8.12.8
- 8.12.7.1
- 8.12.7
- 8.12.6
- 8.12.5
- 8.12.4
- 8.12.3
- 8.12.2
- 8.12.1
- 8.12.0
- 8.11.5
- 8.11.4
- 8.11.3
- 8.11.2
- 8.11.1
- 8.11.0
- 8.10.23
- 8.10.22
- 8.10.21
- 8.10.20
- 8.10.19
- 8.10.18
- 8.10.17
- 8.10.16
- 8.10.15
- 8.10.14
- 8.10.13
- 8.10.12
- 8.10.11
- 8.10.10
- 8.10.9
- 8.10.8
- 8.10.7
- 8.10.6
- 8.10.5
- 8.10.4
- 8.10.3
- 8.10.2
- 8.10.1
- 8.10.0
- 8.9.16
- 8.9.15
- 8.9.14
- 8.9.13
- 8.9.12
- 8.9.11
- 8.9.10
- 8.9.9
- 8.9.8
- 8.9.7
- 8.9.6
- 8.9.5
- 8.9.4
- 8.9.3
- 8.9.2
- 8.9.1
- 8.9.0
- 8.8.11
- 8.8.10
- 8.8.9
- 8.8.8
- 8.8.7
- 8.8.6
- 8.8.5
- 8.8.4
- 8.8.3
- 8.8.2
- 8.8.1
- 8.8.0
- 8.7.1
- 8.7.0
- 8.6.0
- 8.5.2
- 8.5.1
- 8.5
- 8.4.3
- 8.4.2
- 8.4.1
- 8.4
- 8.3.3.1
- 8.3.3
- 8.3.2
- 8.3.1
- 8.3.0
- 8.2.0
- 8.1.0
- 8.0.1
- 8.0
- 7.7.5
- 7.7.4
- 7.7.3
- 7.7.2
- 7.7.1
- 7.6.1
- 7.6.0
- 7.5.2
- 7.5.1
- 7.5.0
- 7.4.5
- 7.4.4
- 7.4.3
- 7.4.2
- 7.4.1
- 7.3.2
- 7.3.1
- 7.3.0
- 7.2.8
- 7.2.7
- 7.2.6
- 7.2.5
- 7.2.4
- 7.2.3
- 7.2.2
- 7.2.1
- 7.1.1
- 7.1.0
- 7.0.11
- 7.0.10
- 7.0.9
- 7.0.8
- 7.0.7
- 7.0.6
- 7.0.5
- 7.0.4
- 7.0.3
- 7.0.2
- 7.0.1
- 7.0
- 6.3.1
- 6.3
- 6.2.2
- 6.2.1
- 6.2.0.1
- 6.2
- 6.1.1
- 6.1
- 6.0
- 5.9.4
- 5.9.3
- 5.9.2
- 5.9.1
- 5.9
- 5.8.8
- 5.8.4
- 5.8
- 5.7.2
- 5.7
- dev-upstream-9.0.2
This package is auto-updated.
Last update: 2025-03-28 10:07:23 UTC
README
What is it?
A PHP library for parsing, formatting, storing and validating international phone numbers. This library is based on Google's libphonenumber.
- Installation
- Documentation
- Online Demo
- Versioning
- Quick Examples
- FAQ
- Generating data
- Integration with frameworks
Installation
PHP versions 8.1 to 8.4 are currently supported.
The PECL mbstring extension is required.
It is recommended to use composer to install the library.
composer require giggsey/libphonenumber-for-php
You can also use any other PSR-4 compliant autoloader.
If you do not use composer, ensure that you also load any dependencies that this project has, such as giggsey/locale.
giggsey/libphonenumber-for-php-lite
If you only want to make use of the core PhoneNumber Util functionality, you can use giggsey/libphonenumber-for-php-lite, which offers a much smaller package size.
PHP Version Policy
This library will be updated to use supported versions of PHP without major version bumps.
Documentation
- PhoneNumber Util
- ShortNumber Info
- Phone Number Geolocation
- Phone Number to Carrier Mapping
- Phone Number to Timezone Mapping
- Phone Number Matcher
- As You Type Formatter
Online Demo
An online demo is available, and the source can be found at giggsey/libphonenumber-example.
Versioning
This library will try to follow the same version numbers as Google. There could be additional releases where needed to fix critical issues that can not wait until the next release from Google.
This does mean that this project may not follow Semantic Versioning, but instead Google's version policy. As a result, jumps in major versions may not actually contain any backwards incompatible changes. Please read the release notes for such releases.
Google try to release their versions according to Semantic Versioning, as laid out of in their Versioning Guide.
Quick Examples
Let's say you have a string representing a phone number from Switzerland. This is how you parse/normalize it into a PhoneNumber object:
$swissNumberStr = "044 668 18 00"; $phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance(); try { $swissNumberProto = $phoneUtil->parse($swissNumberStr, "CH"); var_dump($swissNumberProto); } catch (\libphonenumber\NumberParseException $e) { var_dump($e); }
At this point, swissNumberProto contains:
class libphonenumber\PhoneNumber#9 (7) {
private $countryCode =>
int(41)
private $nationalNumber =>
double(446681800)
private $extension =>
NULL
private $italianLeadingZero =>
NULL
private $rawInput =>
NULL
private $countryCodeSource =>
NULL
private $preferredDomesticCarrierCode =>
NULL
}
Now let us validate whether the number is valid:
$isValid = $phoneUtil->isValidNumber($swissNumberProto); var_dump($isValid); // true
There are a few formats supported by the formatting method, as illustrated below:
// Produces "+41446681800" echo $phoneUtil->format($swissNumberProto, \libphonenumber\PhoneNumberFormat::E164); // Produces "044 668 18 00" echo $phoneUtil->format($swissNumberProto, \libphonenumber\PhoneNumberFormat::NATIONAL); // Produces "+41 44 668 18 00" echo $phoneUtil->format($swissNumberProto, \libphonenumber\PhoneNumberFormat::INTERNATIONAL);
You could also choose to format the number in the way it is dialled from another country:
// Produces "011 41 44 668 1800", the number when it is dialled in the United States. echo $phoneUtil->formatOutOfCountryCallingNumber($swissNumberProto, "US"); // Produces "00 41 44 668 18 00", the number when it is dialled in Great Britain. echo $phoneUtil->formatOutOfCountryCallingNumber($swissNumberProto, "GB");
Geocoder
$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance(); $swissNumberProto = $phoneUtil->parse("044 668 18 00", "CH"); $usNumberProto = $phoneUtil->parse("+1 650 253 0000", "US"); $gbNumberProto = $phoneUtil->parse("0161 496 0000", "GB"); $geocoder = \libphonenumber\geocoding\PhoneNumberOfflineGeocoder::getInstance(); // Outputs "Zurich" echo $geocoder->getDescriptionForNumber($swissNumberProto, "en_US"); // Outputs "Zürich" echo $geocoder->getDescriptionForNumber($swissNumberProto, "de_DE"); // Outputs "Zurigo" echo $geocoder->getDescriptionForNumber($swissNumberProto, "it_IT"); // Outputs "Mountain View, CA" echo $geocoder->getDescriptionForNumber($usNumberProto, "en_US"); // Outputs "Mountain View, CA" echo $geocoder->getDescriptionForNumber($usNumberProto, "de_DE"); // Outputs "미국" (Korean for United States) echo $geocoder->getDescriptionForNumber($usNumberProto, "ko-KR"); // Outputs "Manchester" echo $geocoder->getDescriptionForNumber($gbNumberProto, "en_GB"); // Outputs "영국" (Korean for United Kingdom) echo $geocoder->getDescriptionForNumber($gbNumberProto, "ko-KR");
ShortNumberInfo
$shortNumberInfo = \libphonenumber\ShortNumberInfo::getInstance(); // true var_dump($shortNumberInfo->isEmergencyNumber("999", "GB")); // true var_dump($shortNumberInfo->connectsToEmergencyNumber("999", "GB")); // false var_dump($shortNumberInfo->connectsToEmergencyNumber("911", "GB")); // true var_dump($shortNumberInfo->isEmergencyNumber("911", "US")); // true var_dump($shortNumberInfo->connectsToEmergencyNumber("911", "US")); // false var_dump($shortNumberInfo->isEmergencyNumber("911123", "US")); // true var_dump($shortNumberInfo->connectsToEmergencyNumber("911123", "US"));
Mapping Phone Numbers to carrier
$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance(); $swissNumberProto = $phoneUtil->parse("798765432", "CH"); $carrierMapper = \libphonenumber\PhoneNumberToCarrierMapper::getInstance(); // Outputs "Swisscom" echo $carrierMapper->getNameForNumber($swissNumberProto, "en");
Mapping Phone Numbers to TimeZones
$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance(); $swissNumberProto = $phoneUtil->parse("798765432", "CH"); $timeZoneMapper = \libphonenumber\PhoneNumberToTimeZonesMapper::getInstance(); // returns array("Europe/Zurich") $timeZones = $timeZoneMapper->getTimeZonesForNumber($swissNumberProto);
FAQ
Problems with Invalid Numbers?
This library uses phone number metadata from Google's libphonenumber. If this library is working as intended, it should provide the same result as the Java version of Google's project.
If you believe that a phone number is returning an incorrect result, first test it with libphonenumber via their Online Demo. If that returns the same result as this project, and you feel it is in error, raise it as an Issue with the libphonenumber project.
If Google's Online Demo gives a different result to the libphonenumber-for-php demo, then please raise an Issue here.
Generating data
Generating the data is not normally needed, as this repository will generally always have the up to date metadata.
To compile the data, run the composer script 'build'
composer run build
This build process clones the libphonenumber project at the version specified in METADATA-VERSION.php.
Integration with frameworks
Other packages exist that integrate libphonenumber-for-php into frameworks.
Framework | Packages |
---|---|
Symfony | PhoneNumberBundle |
Laravel | Laravel Phone |
TYPO3 | TYPO3 Phone Extension |
These packages are supplied by third parties, and their quality can not be guaranteed.