danjam / gmail-matcher
Library to check and match the varying formats of Gmail addresses
Requires
- php: ^7.1
Requires (Dev)
- ext-ast: *
- neronmoon/scriptsdev: ^0.1.4
- php-coveralls/php-coveralls: ^2.1
- phpmd/phpmd: ^2.6
- phpstan/phpstan: ^0.11.0
- phpstan/phpstan-phpunit: ^0.11.0
- phpstan/phpstan-strict-rules: ^0.11.0
- phpunit/phpunit: ^7
- povils/phpmnd: ^2.0
- sebastian/phpcpd: ^4.1
- sensiolabs/security-checker: ^5.0
- squizlabs/php_codesniffer: ^3.4
This package is auto-updated.
Last update: 2024-10-25 10:47:17 UTC
README
TL;DR: This is a PHP 7.1+ class that can normalize and compare different formats of the same Gmail address.
Gmail address rules have some caveats:
- Dots in email addresses are disregarded (
foobar@gmail.com
is the same asfoo.bar@gmail.com
). (Source) - Addresses are case insensitive (
FOOBAR@gmail.com
is the same asfoobar@gmail.com
) - The
gmail.com
domain is interchangeable withgooglemail.com
(foobar@gmail.com
is the same asfoobar@googlemail.com
) (Source) - Pluses are allowed in emails (
foobar+baz@gmail.com
)
Because of this it is very possible for a system to have multiple variations of the same Gmail address associated with different users.
This class attempts to address this by providing a match(…)
method to check variations against each other and a normalize(…)
method to retrieve a normalized version of the address. Also exposed is a method for determining if an address is a Gamil address, isGmailAddress(…)
.
Between these three methods it should be possible to check, normalize and store Gmail addresses, for example to prevent duplicate signups within a system.
Install
Via Composer
$ composer require danjam/gmail-matcher
Usage
// 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');