0

I'm using this code to parse out emails from a string:

function get_emails ($str) {
    $pattern = '/([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*\@([a-z0-9])' .
    '(([a-z0-9-])*([a-z0-9]))+' . '(\.([a-z0-9])([-a-z0-9_-])?([a-z0-9])+)/i';

    preg_match ($pattern, $str, $matches);

    return $matches;
}

It works well except when the address has more than one period in domain. So johndoe@yahoo.com works fine but johndoe@yahoo.co.uk get's cut at johndoe@yahoo.co

What can I change to fix this?

Thanks!

  • If your email regex is less than a few thousand characters long, it's incorrect. – hobbs Mar 25 '13 at 18:03
  • I found this on Stack Overflow somewhere. It works with multiple periods: `([A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4})` – David Starkey Mar 25 '13 at 18:05
  • your also assuming names and domains are English! a-z Poor old Renée will be upset http://en.wikipedia.org/wiki/Internationalized_domain_name – Waygood Mar 25 '13 at 18:06
  • http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address – pyInTheSky Mar 25 '13 at 18:10

1 Answers1

0

You could add a + before the end, i.e. +/i.

MikeM
  • 13,156
  • 2
  • 34
  • 47