juanparati/mobile-numbers

国际手机号码验证器

1.4.0 2024-06-21 10:41 UTC

This package is auto-updated.

Last update: 2024-09-21 11:12:53 UTC


README

Build Status

MobileNumbers

这是什么?

一个验证和解析手机号码的库。

目前只验证以下国家的手机号码

欢迎fork此项目并添加新的国家。

工作原理

验证手机号码

    Validator::country('ES')->isValid('651365485');

可以验证包含国际国家代码的手机号码

    Validator::country('DK')->isValid('+4560514180');
    Validator::country('SE')->isValid('0046767164315');

库提供辅助函数将不同的手机号码格式转换为E.164

    $validator = Validator::country('PL');
    $raw_number = '(669) 823-955';
    
    $e164_number = $validator->helper->convertToE164($mobile_number);   // Convert number to "669823955"
    echo $validator->isValid($e164_number) ? 'Valid!' : 'Not Valid!');
    
    
    $validator = Validator::country('SE');
    $raw_number = '(+46) 7 37322 0-66';
    
    $e164_number = $validator->helper->convertToE164($mobile_number);   // Convert number to "+46737322066"
    echo $validator->isValid($e164_number) ? 'Valid!' : 'Not Valid!');

也可以向没有国家代码的号码添加有效的国家代码

    Validator::country('SE')->addCountryCode('0737321066');    // +46737321066
    Validator::country('DK')->addCountryCode('60515290');      // +4560515290
    
    Validator::country('DK')->addCountryCode('+4560515290');   // +4560515290  (Country code is not added again)
    Validator::country('DK')->addCountryCode('004560515290');  // 004560515290 (Country code is not added again)

或去除国家代码

    Validator::country('SE')->stripCountryCode('0046737321066');   // 0737321066
    Validator::country('FI')->stripCountryCode('00358411234567');  // 0411234567
    Validator::country('ES')->stripCountryCode('+34670862595');    // 670862595

其他方法

    Validator::countr('ES')->helper->hasCountryCode('0030670862595'); // True
    Validator::country('ES')->hasValidCountryCode('0030670862595');  // False

定义列表

使用辅助方法"getAllDefinitions",可以获取所有定义中使用的所有代码的信息

    $definitions = Helper::getAllDefinitions();

定义包括国家前缀代码、国家代码和国家标志信息。

识别手机号码

使用辅助方法"identifyNumber",可以识别属于哪个国家的手机号码。手机号码应包含国际前缀。

示例

    Helper::identifyNumber('+4560514180');  // Returns DK

添加你的国家定义

  1. fork此项目
  2. 在./src/Definitions中添加新的定义
  3. 在./tests/Definitions中添加新的单元测试

赞助商和贡献者