128

This is the regex I'm trying to use:

/^(\w|\.|-)+?@(\w|-)+?\.\w{2,4}($|\.\w{2,4})$/gim

I found it on this site, and it works great when I try it out there. But as soon as I place it in my code, I get this message:

Warning: preg_match() [function.preg-match]: Unknown modifier 'g' in C:\xampp\htdocs\swebook\includes\classes.php on line 22

Can anyone explain what's wrong, and why it's working on that website and not in my code?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Nike
  • 1,479
  • 3
  • 10
  • 9
  • 4
    FYI, your regexp is gonna validate OK these type of mails: "-.-.-.--.@--some--domain--.com" and these type of VALID mails will NOT validate: "info@subdomain.domain.com". Last but not least \w is VERY DANGEROUS cause PHP preg uses Perl Compatible Regular Expressions and NOT POSIX like Javascript. If PHP runs on server that is not US/UK localized \w is gonna match also accented letters so you would match "àèìòù@domain.com" has a valid mail. Read here for more details: http://it.php.net/manual/en/function.preg-replace.php#92443 – Marco Demaio Aug 26 '10 at 20:30

1 Answers1

222

There is no modifier g for preg_match. Instead, you have to use the preg_match_all function.

So instead of:

preg_match("/^(\w|\.|-)+?@(\w|-)+?\.\w{2,4}($|\.\w{2,4})$/gim", ....)

use:

preg_match_all("/^(\w|\.|-)+?@(\w|-)+?\.\w{2,4}($|\.\w{2,4})$/im", ....)
Sharanya Dutta
  • 3,981
  • 2
  • 17
  • 27
codaddict
  • 445,704
  • 82
  • 492
  • 529