john
john

Reputation: 535

php regular expression : How to fetch all the email address in a paragraph

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

Thanks a lot

Upvotes: 0

Views: 1749

Answers (5)

Pramendra Gupta
Pramendra Gupta

Reputation: 14873

<?php
$paragraph="this is test [email protected] [email protected] 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]);
?>

Upvotes: 0

shamittomar
shamittomar

Reputation: 46692

Look here for Email regexes:

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

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

Upvotes: 2

Russell Dias
Russell Dias

Reputation: 73282

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.

Upvotes: 0

Kranu
Kranu

Reputation: 2567

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.

Upvotes: 0

Jet
Jet

Reputation: 1325

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

Upvotes: -1

Related Questions