danjam/gmail-matcher

用于检查和匹配Gmail地址不同格式的库

1.0.0 2019-02-08 16:14 UTC

This package is auto-updated.

Last update: 2024-08-25 10:24:06 UTC


README

Latest Stable Version Build Status Coverage Status Infection MSI

TL;DR:这是一个PHP 7.1+类,可以标准化和比较同一Gmail地址的不同格式。

Gmail地址规则有一些注意事项

  • 电子邮件中的点被忽略(foobar@gmail.comfoo.bar@gmail.com 相同)。(《[来源](https://support.google.com/mail/answer/7436150)》)
  • 地址不区分大小写(FOOBAR@gmail.comfoobar@gmail.com 相同)
  • gmail.com 域名可以与 googlemail.com 互换(foobar@gmail.comfoobar@googlemail.com 相同)。(《[来源](https://support.google.com/mail/answer/10313)》)
  • 电子邮件中允许使用加号(foobar+baz@gmail.com

因此,系统可能存在与不同用户关联的同一Gmail地址的多个变体。

此类尝试通过提供 match(…) 方法来检查变体之间的比较以及 normalize(…) 方法来检索地址的标准化版本来解决这个问题。还提供了一个确定地址是否为Gmail地址的方法,即 isGmailAddress(…)

通过这三个方法,应该可以检查、标准化并存储Gmail地址,例如防止系统中的重复注册。

安装

通过 Composer

$ composer require danjam/gmail-matcher

用法

// instantiate new matcher
$gmailMatcher = new \danjam\GmailMatcher\GmailMatcher();

// you can also specify the domain used when normalizing. Must be one of gmail.com, googlemail.com. Defaults to gmail.com
$gmailMatcher = new \danjam\GmailMatcher\GmailMatcher('googlemail.com');

// normalizes the address - returns foo@gmail.com
$gmailMatcher->normalize('F.O.O@gmail.com');

// check if two addresses are the same - returns true
$gmailMatcher->match('F.O.O@gmail.com', 'foo@gmail.com');

// returns false
$gmailMatcher->match('foo@gmail.com', 'bar@gmail.com');

// returns false - addresses with + should NOT be treated as the same address
$mailMatcher->match('foo@gmail', 'foo+bar@gmail.com');

// multiple addresses
$gmailMatcher->match('bar@gmail.com', 'b.a.r@gmail.com', 'bar@googlemail.com', ...);

// validate the address is a Gmail address - returns true
$gmailMatcher->isGmailAddress('foo@gmail.com');

// returns false
$gmailMatcher->isGmailAddress('bar@example.com');