0

I have a string and I want to retrieve all e-mails contained in that string.

I can get the @domain.com part with this code:

 preg_match_all('{@(([^ ]*)\.com)}', $string, $matches);
 print_r($matches[1]);

What I want to get is:

name@domain.com

I used this to make the PHP work: getting emails out of string - regex syntax + preg_match_all

Community
  • 1
  • 1
2Noob2Good
  • 159
  • 1
  • 12

1 Answers1

4

You must use a complete email regex :

^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$

You can find this regex here : http://www.regular-expressions.info/email.html

  • Thanks for the quick reply. But my array is empty with that regex code :z – 2Noob2Good Nov 26 '13 at 17:03
  • Adding `^$` to the expression will make it fail since it means that the string should only contain an email address. I'm wondering why this is upvoted that much ... – HamZa Nov 26 '13 at 18:46