适用于 PHP 的 libphonenumber

PHP 版本的谷歌电话号码处理库。(PHP version of Google's phone number handling library.)

Github星跟蹤圖

适用于 PHP 的 libphonenumber

它是什么?

用于解析、格式化、存储和验证国际电话号码的PHP库。此库基于 Google 的 libphonenumber

安装

目前支持 PHP 5.3 到 PHP 7.4 版本。

PECL mbstring 扩展是必需的。

建议使用 Composer 来安装这个库。

$ composer require giggsey/libphonenumber-for-php

你也可以使用任何其他兼容 PSR-4 的 autoloader。

如果您不使用 composer,请确保你也加载了这个项目的任何依赖关系,比如 giggsey/locale

文档

在线演示

有一个在线演示,其源代码可以 giggsey/libphonenumber-example 上找到。

功能亮点

  • 解析/格式化/验证全球所有国家/地区的电话号码。
  • getNumberType -- 根据数字本身获取数字的类型;能够区分固定线路, 移动, 免费, 保费率, 共享成本, VoIP 和个人号码 (在可行的情况下)。
  • isNumberMatch -- 获取两个数字是否相同的置信度。
  • getExampleNumber/getExampleNumberByType -- 为所有国家/地区提供有效的示例数字,并可选择指定需要哪种类型的示例电话号码。
  • isValidNumber -- 使用长度和前缀信息完整验证某个地区的电话号码。
  • PhoneNumberOfflineGeocoder -- 提供与电话号码相关的地理信息。
  • PhoneNumberToTimeZonesMapper -- 提供与电话号码相关的时区信息。
  • PhoneNumberToCarrierMapper -- 提供与电话号码相关的运营商信息。

版本

这个库会尝试使用与 Google 相同的版本号。可能需要额外的版本来解决不能等到Google 下一个版本发布的关键问题。

这确实意味着这个项目可能不遵循语义版本,而是 Google 的版本策略。因此,主要版本的跳转实际上可能不包含任何向后不兼容的更改。 请阅读此类版本的发行说明。

Google尝试根据语义版本发布他们的版本,正如他们的版本控制指南

快速示例

假设你有一个代表瑞士电话号码的字符串。这是你解析/标准化为 PhoneNumber 对象的方式:

$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);
}

此时,swissNumberProto 包含:

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
}

现在让我们验证一下数字是否有效:

$isValid = $phoneUtil->isValidNumber($swissNumberProto);
var_dump($isValid); //true

格式化方法支持一些格式,如下所示:

//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);

您也可以选择以另一个国家/地区拨打电话的方式格式化电话号码。

//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");

地理编码

$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"));

将电话号码映射到运营商

$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
$swissNumberProto = $phoneUtil->parse("798765432", "CH");
$carrierMapper = \libphonenumber\PhoneNumberToCarrierMapper::getInstance();
//Outputs "Swisscom"
echo $carrierMapper->getNameForNumber($swissNumberProto, "en");

将电话号码映射到时区

$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
$swissNumberProto = $phoneUtil->parse("798765432", "CH");
$timeZoneMapper = \libphonenumber\PhoneNumberToTimeZonesMapper::getInstance();
//returns array("Europe/Zurich")
$timeZones = $timeZoneMapper->getTimeZonesForNumber($swissNumberProto);

常见问题

遇到无效号码的问题?

这个库使用了 Google libphonenumber 中的电话号码元数据。如果该库按预期工作,它应该提供与 Google 项目的 Java 版本相同的结果。

首先通过他们的在线演示

如果你认为一个电话号码返回了一个不正确的结果,请先通过它们的 在线演示libphonenumber 进行测试。如果它返回的结果和这个项目一样,而你又觉得它有错误,那么就把它作为 libphonenumber 项目的一个问题提出来。

如果 Google 的 在线演示 给出了与 libphonenumber-for-php demo 演示不同的结果,那么请在这里提出一个问题。

生成数据

通常不需要生成数据,因为此存储库通常始终具有高达数据元数据。

如果您确实需要生成数据,则命令由 Phing 提供。确保安装了所有dev composer依赖项,然后运行

$ vendor/bin/phing compile

这个编译过程以 METADATA-VERSION.txt 中指定的版本克隆 libphonenumber

与框架集成

还有其他一些将 libphonenumber-for-php 整合到框架中的软件包。

框架 软件包
Symfony PhoneNumberBundle
Laravel Laravel Phone
Yii2 PhoneInput
Kohana PhoneNumber
TYPO3 TYPO3 Phone Extension

这些软件包由第三方提供,其质量无法保证。


(Second edition: vz revised at 2020.08.30)

主要指標

概覽
名稱與所有者giggsey/libphonenumber-for-php
主編程語言PHP
編程語言PHP (語言數: 1)
平台Linux, Mac, Windows
許可證Apache License 2.0
所有者活动
創建於2013-03-28 13:36:31
推送於2025-04-11 07:42:24
最后一次提交2025-04-11 08:41:09
發布數257
最新版本名稱9.0.3 (發布於 )
第一版名稱5.7 (發布於 )
用户参与
星數4.9k
關注者數109
派生數470
提交數1.2k
已啟用問題?
問題數322
打開的問題數3
拉請求數328
打開的拉請求數0
關閉的拉請求數44
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?

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 5.3 up to PHP 7.3 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.

Documentation

Online Demo

An online demo is available, and the source can be found at giggsey/libphonenumber-example.

Highlights of functionality

  • Parsing/formatting/validating phone numbers for all countries/regions of the world.
  • getNumberType - gets the type of the number based on the number itself; able to distinguish Fixed-line, Mobile, Toll-free, Premium Rate, Shared Cost, VoIP and Personal Numbers (whenever feasible).
  • isNumberMatch - gets a confidence level on whether two numbers could be the same.
  • getExampleNumber/getExampleNumberByType - provides valid example numbers for all countries/regions, with the option of specifying which type of example phone number is needed.
  • isValidNumber - full validation of a phone number for a region using length and prefix information.
  • PhoneNumberOfflineGeocoder - provides geographical information related to a phone number.
  • PhoneNumberToTimeZonesMapper - provides timezone information related to a phone number.
  • PhoneNumberToCarrierMapper - provides carrier information related to a phone number.

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 data metadata.

If you do need to generate the data, the commands are provided by Phing. Ensure you have all the dev composer dependencies installed, then run

$ vendor/bin/phing compile

This compile process clones the libphonenumber project at the version specified in METADATA-VERSION.txt.

Integration with frameworks

Other packages exist that integrate libphonenumber-for-php into frameworks.