blobfolio / blob-phone
Google libphonenumber的轻量级实现,用于PHP。
Requires
- php: >= 7.0
- ext-bcmath: *
- ext-dom: *
- ext-filter: *
- ext-hash: *
- ext-json: *
- ext-mbstring: *
- blobfolio/blob-common: *
Requires (Dev)
- phpunit/phpunit: 5.7.*
README
blob-phone 是 Google 的 libphonenumber 的轻量级实现,用于PHP。
目录
特性
blob-phone 主要用于验证和格式化任意国际电话号码(例如 +1 201-555-0123)。在这个过程中,它还收集有关号码的以下信息:
- 国家
- 洲
- 类型(例如
mobile,voip等)
已从 libphonenumber 中移除其他数据和方法,以减小库的大小。对于为PHP编写的更全面的 libphonenumber 实现,请参阅 libphonenumber-for-php。
安装
blob-phone的PHP库需要
- PHP 7.0+
- blobfolio/blob-common
使用Composer安装
composer require "blobfolio/blob-phone:dev-master"
使用
开始使用
$phone = new blobfolio\phone\phone($phone_number, $country_code='US');
构造函数接受两个参数
- (string) 电话号码(已格式化或未格式化);
- (string) (可选) ISO国家代码(疑似);默认:
"US"
提供可能的发源地有助于识别,因此建议这样做。否则,您的号码可能会出现在地球的另一边。
blob-phone使用暴力搜索技术来识别和格式化电话号码,优先级如下:
- 提供的国家;
- 具有相同前缀的其他国家;
- 同一洲的其他国家;
- 其他所有地方,按字母顺序排列;
检查
if ($phone->is_phone($types=array())) { ... }
此方法接受一个可选的数组,包含电话号码的类型,如果您正在寻找特定内容。类型检测可能成功也可能失败,因此如果您需要高精度,您应通过其他方式验证。
可能类型有:
"fixed",即固定电话"mobile""pager""personal_number""premium_rate",例如您最喜欢的派对线路"shared_cost""toll_free""voicemail""voip",例如Google Voice
此方法返回 TRUE 或 FALSE。
提取
// If you only want the number, you can typecast it: $formatted = (string) $phone; //+598 9423 1234 // If you want more details: print_r($phone->get_data()); /* Array( [country] => CA [prefix] => 1 [region] => North America [types] => Array( [0] => fixed [1] => mobile ) [number] => +1 204-234-5678 ) */
现在全部一起...
$phone = '1012345678'; $country = 'CN'; $phone = (string) blobfolio\phone\phone($phone, $country); if (strlen($phone)) { echo "Your pretty phone number is: $phone"; } else { echo "I don't think that was right..."; }
JavaScript
blob-phone 也可以作为无依赖的JavaScript库使用。要使用它,只需在您的项目中包含 lib/js/blob-phone.min.js 即可。
此库有一个方法:blobPhone.parse(),它接受两个参数
- (string) 电话号码(已格式化或未格式化);
- (string) (可选) ISO国家代码(疑似);默认:
"US"
提供可能的发源地有助于识别,因此建议这样做。否则,您的号码可能会出现在地球的另一边。
如果无法解析号码,则返回 FALSE,或者返回包含号码详细信息的对象,如下所示:
var parsed = blobPhone.parse(2015550123); /* { country: "US", number: "+1 201-555-0123", prefix: 1, region: "North America", types: [ "fixed", "mobile" ] } */
许可
版权所有 © 2018 Blobfolio, LLC <hello@blobfolio.com>
本作品是免费的。您可以在“做什么都行公共许可证”第2版条款下重新分发和/或修改。
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
捐赠
| 如果您觉得这个作品很有用,并希望财务上给予支持,比特币小费总是受欢迎的! 1Af56Nxauv8M1ChyQxtBe1yvdp2jtaB1GF |