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
fetch email
Moderator: General Moderators
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: fetch email
Try this:
HTH
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);
}
?>-
devendra-m
- Forum Contributor
- Posts: 111
- Joined: Wed Sep 12, 2007 3:16 am
Re: fetch email
preg_match_all('/<([^<>]*@[^<>]*)>/',$string,$match);
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: fetch email
That would also match a String <!-- @param >, <@> and a lot more bogus strings.devendra-m wrote:preg_match_all('/<([^<>]*@[^<>]*)>/',$string,$match);
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
Your pattern is workable for all email addresses but the pattern I have given is specific to the pattern of the string given.
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: fetch email
Not all: but it will do in almost all cases.devendra-m wrote:Your pattern is workable for all email addresses
Sure, and as I previously mentioned also quite a bit invalid ones.devendra-m wrote:but the pattern I have given is specific to the pattern of the string given.