giggsey/libphonenumber-for-php-lite
Composer 安装命令:
composer require giggsey/libphonenumber-for-php-lite
包简介
A lite version of giggsey/libphonenumber-for-php, which is a PHP Port of Google's libphonenumber
README 文档
README
What is it?
A PHP library for parsing, formatting, storing and validating international phone numbers. This library is based on Google's libphonenumber.
This is a lite version that only includes the core Phone Number Utils. To make full use of the library, including geolocation, carrier information and short number info, use giggsey/libphonenumber-for-php
Installation
PHP versions 8.1 and above are supported.
The PECL mbstring extension is required.
It is recommended to use composer to install the library.
composer require giggsey/libphonenumber-for-php-lite
You can also use any other PSR-4 compliant autoloader.
PHP Version Policy
This library will be updated to use supported versions of PHP only.
Documentation
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.
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");
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 on the giggsey/libphonenumber-for-php project.
If giggsey/libphonenumber-for-php differs from this library, please raise an issue here instead.
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.
giggsey/libphonenumber-for-php-lite 适用场景与选型建议
giggsey/libphonenumber-for-php-lite 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 16.75M 次下载、GitHub Stars 达 90, 最近一次更新时间为 2022 年 10 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「geocoding」 「geolocation」 「validation」 「mobile」 「phonenumber」 「libphonenumber」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 giggsey/libphonenumber-for-php-lite 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 giggsey/libphonenumber-for-php-lite 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 giggsey/libphonenumber-for-php-lite 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Maps in minutes. Powered by the Google Maps API.
Yii2 map input widget. Allows you to select geographcal coordinates via a human-friendly inteface.
GISCO Geocoding Provider for Geocoder PHP.
The official PHP library for apiip.net that allowing customers to automate IP address validation and geolocation lookup in websites, applications and back-office system. Visit our API docs at https://apiip.net/documentation
Laravel integration for Nominatim Geocoding API Client
Google Maps PHP SDK.
统计信息
- 总下载量: 16.75M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 92
- 点击次数: 33
- 依赖项目数: 41
- 推荐数: 4
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2022-10-31