Retour au classement

giggsey/libphonenumber-for-php

PHPgiggsey.com/libphonenumber/

PHP version of Google's phone number handling library

phone-numberphplibphonenumberhacktoberfest
Croissance des étoiles
Étoiles
5k
Forks
479
Croissance hebdomadaire
Issues
2
2k4k
juil. 2013nov. 2017mars 2022juil. 2026
ArtefactsPackagistcomposer require giggsey/libphonenumber-for-php
README

libphonenumber for PHP Build Status Coverage Status

Total Downloads Downloads per month Latest Stable Version License

What is it?

A PHP library for parsing, formatting, storing and validating international phone numbers. This library is based on Google's libphonenumber.

Installation

PHP versions 8.1 to 8.5 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

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.

Dépôts similaires
kdeldycke/awesome-falsehood

😱 Falsehoods Programmers Believe in

Creative Commons Zero v1.0 Universalfalsehoodsphone-number
27.6k643
sundowndev/phoneinfoga

Information gathering framework for phone numbers

GoGo ModulesGNU General Public License v3.0information-gatheringphone-number
sundowndev.github.io/phoneinfoga/
17.1k5.2k
HunxByts/GhostTrack

Useful tool to track location or mobile number

PythonPyPIfyphacking
14.5k1.9k
jackocnr/intl-tel-input

For entering, formatting, and validating international telephone numbers. Available in vanilla JavaScript, or as React, Vue, Angular, and Svelte components.

TypeScriptnpmMIT Licenseinternational-telephonephone-number
intl-tel-input.com
8.2k2k
marmelroy/PhoneNumberKit

A Swift framework for parsing, formatting and validating international phone numbers. Inspired by Google's libphonenumber.

SwiftMIT Licensephone-numbergoogle-libphonenumber
5.4k856
gommzystudio/device-activity-tracker

A phone number can reveal whether a device is active, in standby or offline (and more). This PoC demonstrates how delivery receipts + RTT timing leak sensitive device-activity patterns. (WhatsApp / Signal)

TypeScriptnpmOtherphone-numbersignal
5k692
VincentSit/ChinaMobilePhoneNumberRegex

Regular expressions that match the mobile phone number in mainland China. / 一组匹配中国大陆手机号码的正则表达式。

MIT Licensephone-numberregular-expression
4.8k508
TermuxHackz/X-osint

This is an Open source intelligent framework ie an osint tool which gathers valid information about a phone number, user's email address, perform VIN Osint, and reverse, perform subdomain enumeration, able to find email from a name, and so much more. Best osint tool for Termux and linux

PythonPyPIGNU General Public License v3.0email-osintip-osint
termuxhackz.github.io/How-to-get-information-using-Xosint.html
2.5k313
iziz/libPhoneNumber-iOS

iOS port of Google's libphonenumber with Objective-C core, Swift facade, SwiftUI input, and CocoaPods/SPM support

Objective-CApache License 2.0libphonenumbercocoapods
github.com/iziz/libPhoneNumber-iOS/releases/latestc
2.4k482
AzizKpln/Moriarty-Project

This tool gives information about the phone number that you entered.

PythonPyPIMIT Licenseosintosint-tool
2k295
megadose/ignorant

ignorant allows you to check if a phone number is used on different sites like snapchat, instagram.

PythonPyPIGNU General Public License v3.0phone-numbertrio
1.9k284
bellingcat/telegram-phone-number-checker

Check if phone numbers are connected to Telegram accounts.

PythonPyPIMIT Licensecommand-lineopen-source-research
pypi.org/project/telegram-phone-number-checker/
1.7k278