fetch email

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
rupam_jaiswal
Forum Newbie
Posts: 22
Joined: Thu Jun 05, 2008 12:54 am

fetch email

Post by rupam_jaiswal »

Hi,
I have to write a regular expression to fetch a email from the given type of string.

Eg "Customer Service" <tina_data@rediffmail.com>
so I have to extract email I,e tina_data@rediffmail.com

But this can also be
“<customer service>” <tina_data@rediffmail.com.

So I have to find string like search first ‘<’ after second double quote(” “ )
Thanks in advance
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: fetch email

Post by prometheuzz »

Try this:

Code: Select all

<?php
$text = '
Hi,
I have to write a regular expression to fetch a email from the given type of string.
 
Eg "Customer Service" <tina_data@rediffmail.com>
so I have to extract email I,e tina_data@rediffmail.com
 
But this can also be
"<customer service>" <tina_data@rediffmail.com.
 
So I have to find string like search first "<" after second double quote(" " )
Thanks in advance
';
 
$email_regex = '/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/i';
 
if(preg_match_all($email_regex, $text, $matches)) {
    print_r($matches);
}
?>
HTH
devendra-m
Forum Contributor
Posts: 111
Joined: Wed Sep 12, 2007 3:16 am

Re: fetch email

Post by devendra-m »

preg_match_all('/<([^<>]*@[^<>]*)>/',$string,$match);
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: fetch email

Post by prometheuzz »

devendra-m wrote:preg_match_all('/<([^<>]*@[^<>]*)>/',$string,$match);
That would also match a String <!-- @param >, <@> and a lot more bogus strings.
In what way did you find your suggestion better than that what was already suggested?
devendra-m
Forum Contributor
Posts: 111
Joined: Wed Sep 12, 2007 3:16 am

Re: fetch email

Post by devendra-m »

Your pattern is workable for all email addresses but the pattern I have given is specific to the pattern of the string given.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: fetch email

Post by prometheuzz »

devendra-m wrote:Your pattern is workable for all email addresses
Not all: but it will do in almost all cases.
devendra-m wrote:but the pattern I have given is specific to the pattern of the string given.
Sure, and as I previously mentioned also quite a bit invalid ones.
Post Reply