php regular expression : How to fetch all the email address in a paragraph
I want to fetch a开发者_JS百科ll the email address in a paragraph. could you please help me
Thanks a lot
Look here for Email regexes:
http://www.regular-expressions.info/email.html
It even has the "The Official Standard: RFC 2822" supporting email regexes.
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.
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.
<?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]);
?>
Try this:
regEx: /[-.\w]+@[-.\w]+/i
精彩评论