0

I want to fetch all the email address in a paragraph. could you please help me

Thanks a lot

shamittomar
  • 46,210
  • 12
  • 74
  • 78
john
  • 535
  • 7
  • 23
  • Add the regex tag, you will get other responses. – Brad Aug 29 '10 at 05:01
  • 2
    You'll probably also get better responses if you show you've made an effort. That's not a strict requirement on SO, but people are usually more willing to answer if they feel they're helping you rather than spoonfeeding you. – Cam Aug 29 '10 at 05:06
  • 1
    This is for Perl style regex, but it does give you an idea of the complixities - http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html . Hoever http://www.regular-expressions.info/email.html is a more practical start. – Peter Ajtai Aug 29 '10 at 05:33

5 Answers5

2

Look here for Email regexes:

http://www.regular-expressions.info/email.html

It even has the "The Official Standard: RFC 2822" supporting email regexes.

shamittomar
  • 46,210
  • 12
  • 74
  • 78
0

You can use the preg_match_all function. The syntax would be like this:

//Filter our the email addresses and store it in $emails
preg_match($regexp,$text,$emails);

//Print out the array of emails
print_r($emails);

where:

  • $regexp is a regular expression for an email
  • $text is the paragraph of text
  • $emails is the output array of email addresses

A regular expression can vary depending on how loose or tight you want it to be. Try crafting your own, so that it follows your standards, but if you're having trouble, you can just Google Email Regular Expressions.

P.S. I hope you're not planning to use this to spam somehow.

Kranu
  • 2,557
  • 16
  • 22
0

As suggested by others, please put more for of an effort into your questions.

The following is a very basic implementation of what you're after. It does ignore some email types, but hey, its the same amount of effort you put in.

  $matches = preg_match_all(
    "/[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}/i",
    $text,
    $emails
  );

$text is your paragraph. $emails is the array of matched values.

Russell Dias
  • 70,980
  • 5
  • 54
  • 71
0
<?php
$paragraph="this is test abc@dd.com nobody@dd.co.jp testing";
$pattern="/([\s]*)([_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*([ ]+|)@([ ]+|)([a-zA-Z0-9-]+\.)+([a-zA-Z]{2,}))([\s]*)/i"; preg_match_all($pattern, $paragraph, $matches);

print_r($matches[0]);
?>
Pramendra Gupta
  • 14,667
  • 4
  • 33
  • 34
-1

Try this: regEx: /[-.\w]+@[-.\w]+/i

Jet
  • 1,283
  • 10
  • 7