行业资讯 Laravel 中的电话号码格式、验证和模型转换

Laravel 中的电话号码格式、验证和模型转换

169
 

Laravel -Phone包使在 PHP 和 Laravel 中处理电话号码变得轻而易举,提供验证规则、属性转换、实用帮助程序等。

您是否曾经围绕支持多个国家/地区的电话号码建立过验证?该软件包内置了有用的验证规则,可以轻松验证任何国家/地区的号码。您可以指定可接受的国家/地区代码格式,但同时接受有效的“国际”号码:

// Validate either USA or Belguim
Validator::make($request->all(), [
    'phone_number' => 'phone:US,BE',
]);
 
// Validate US specifically, but also accept other countries
Validator::make($request->all(), [
    'phone_number' => 'phone:US,INTERNATIONAL',
]);
 
// Use the Phone rule
Validator::make($request->all(), [
    'phone_number' => (new Phone)->country(['US', 'BE']),
]);
 
// Match country code against another data field
Validator::make($request->all(), [
    'phone_number' => (new Phone)->countryField('custom_country_field'),
    'custom_country_field' => 'required_with:phone_number',
]);

该软件包在底层使用Google 电话号码处理库的 PHP 端口,该库具有强大的解析、格式化和验证功能,可用于处理 PHP 中的电话号码:

// Formatting examples
 
$phone = new PhoneNumber('012/34.56.78', 'BE');
 
$phone->format($format);       // Custom formatting
$phone->formatE164();          // +3212345678
$phone->formatInternational(); // +32 12 34 56 78
$phone->formatRFC3966();       // +32-12-34-56-78
$phone->formatNational();      // 012 34 56 78

您可以了解有关此软件包的更多信息、获取完整的安装说明并在 GitHub 上查看源代码以获得有关此包的完整文档。

更新:2024-03-08 22:50:35 © 著作权归作者所有
QQ
微信