Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Preg_replace plus and dots in gmail

spammers driving me crazy. Don’t know how Google allows infinite email aliases just by adding a dot or a plus. So I need to kind of sanitize emails like these:

  • john.brian@gmail.com
  • henix..mail+@gmail.com
  • sar+ah+.black+.+@gmail.com

to this:

  • johnbrian@gmail.com
  • henixmail@gmail.com
  • sarahblack@gmail.com

The regular expression that works is this:
(?:\.|\+.*)(?=.*?@gmail\.com)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

My email function is $mail[’email’], so how do I match dots and plus in this function if found then remove them and clean them up?

What i’ve found by now…
Works but don’t know how to if this then that

preg_match( '/@gmail\.com$/', $mail['email']
preg_replace('^(?:\.|\+.*)(?=.*?@gmail\.com)$',  $mail['email'] );

>Solution :

Your pattern in anchored, so using ^(?:\.|\+.*)(?=.*?@gmail\.com)$ will not find separate matches.

Also the +.* could match more than only the plus sign, removing more than expected.

You could match either dot or a plus sign using a character class [.+] and assert gmail.com to the right using a positive lookahead, matching only non whitspace chars except an @ until you encounter the first occurrence of @

See the matches on regex101.

For example:

$pattern = "/[.+](?=[^\s@]*@gmail\.com)/";

$mail = ["email" => "sar+ah+.black+.+@gmail.com"];
$mail['email'] = preg_replace($pattern, '',  $mail['email'] );

print_r($mail['email']);

Output

sarahblack@gmail.com
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading