/    Sign up×
Community /Pin to ProfileBookmark

Finding email addresses

I am using the following to locate email addresses from a text file:

if (preg_match(“/([a-zA-Z0-9-_]+)*@([a-z0-9-]+)(.[a-zA-Z0-9-]+)*(.[a-zA-Z]{2,4})/”,$x))

The above works in finding email addresses like the following:
[email][email protected][/email]

but is unable to identify the following:

[email][email protected][/email] or [email][email protected][/email]

thanks in advance.

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@felgallMay 06.2007 — You are not allowing for an uppercase letter immediately after the @
Copy linkTweet thisAlerts:
@NogDogMay 06.2007 — You are also not allowing an address like [email][email protected][/email], which is valid.

If you want to be really thorough:
[code=php]
$qtext = '[^\x0d\x22\x5c\xa6-\xff]';
$dtext = '[^\x0d\x5b-\x5d\xa6-\xff]';
$atom = '[^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c'.
'\x3e\x40\x5b-\x5d\xa6-\xff]+';
$quoted_pair = '\x5c[\x00-\xa5]';
$domain_literal = "\x5b($dtext|$quoted_pair)*\x5d";
$quoted_string = "\x22($qtext|$quoted_pair)*\x22";
$domain_ref = $atom;
$sub_domain = "($domain_ref|$domain_literal)";
$word = "($atom|$quoted_string)";
$domain = "$sub_domain(\x2e$sub_domain)*";
$local_part = "$word(\x2e$word)*";
$addr_spec = "$local_part\x40$domain";
if(preg_match("/$addr_spec/", $x, $matches)) {
[/code]

See http://iamcal.com/publish/articles/php/parsing_email/ for more info.
×

Success!

Help @targeting spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...