Page 1 of 1
fetch email
Posted: Wed Jul 16, 2008 6:22 am
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
Re: fetch email
Posted: Wed Jul 16, 2008 6:40 am
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
Re: fetch email
Posted: Fri Aug 08, 2008 5:00 am
by devendra-m
preg_match_all('/<([^<>]*@[^<>]*)>/',$string,$match);
Re: fetch email
Posted: Fri Aug 08, 2008 8:25 am
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?
Re: fetch email
Posted: Sun Aug 10, 2008 11:17 pm
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.
Re: fetch email
Posted: Tue Aug 12, 2008 2:57 am
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.