tags:

views:

1246

answers:

8

In php, I have a string like this:

$string = "[email protected] MIME-Version: bla bla bla";

How do i get the email address only? Is there any easy way to get the value??

EDIT:

THANKS guys. Using regular expression solved my problem. You guys are the BEST!!

+1  A: 

If the email address is always at the front of the string, the easiest way to get it is simply to split the string on all instances of the space character, and then just take the first value from the resulting array.

Of course, make sure to check it is something resembling an email address before you use it.

See the PHP 'split' function for details.

ravuya
A: 

Have a look at Regular expressions in PHP.

With regular expressions you can identify any text pattern in a given string. They are incredibly useful. So even though you can stick with copy-pasting a code snippet from another answer for now, you should consider digging a little more into it.

It can be a bit complex at first but it's definitely worth the effort.

anderstornvig
A: 

If it's really space-separated:

php > $matches = array();
php > preg_match('/^[^ ]*/', $string, $matches);
php > print_r($matches[0]);
[email protected]
Matthew Flaschen
A: 

Match to a regular expression like:

([A-Za-z0-9-]+)@([A-Za-z0-9])\.([a-z]{3})

or something similar

SP
+1  A: 

Try something like that:

$string = "[email protected] MIME-Version: bla bla bla";
$matches = array();
preg_match('/(.*)@(.*)\.[a-zA-Z]{1,9}/',$string,$matches);
echo $matches[0];
Wadih M.
You must not be a fan of the Staatliche Museen zu Berlin ([email protected])
Matthew Flaschen
What makes you say that? My e-mail matching accepts anything of the form [email protected] where x,y are anything and z is a 4 letters string.
Wadih M.
Museum is 6 letters. ;)
Sean Nyman
Right :)I fixed it for .museum e-mails and other TLDs that can go up to 9 characters.
Wadih M.
[email protected] is a valid email address. For some serious obsessive treatment of the subject take a look things like this:http://www.dominicsayers.com/isemail/
Colin Pickard
Those users are most probably not the kind of users I'd want on my website.
Wadih M.
+1  A: 

Email addresses are really tricky to filter using regular expressions because there are so many possible allowable characters. It can be done, but you may have to tweak it some to get exactly what you need.

You could start with something like this:

$string = "[email protected] MIME-Version: bla bla bla";
$matches = array();
$pattern = '/[A-Za-z0-9_-]+@[A-Za-z0-9_-]+\.([A-Za-z0-9_-][A-Za-z0-9_]+)/'
preg_match($pattern,$string,$matches);

And then $matches should contain your email address.

Gabriel Hurley
+3  A: 

If you're not sure which part of the space-separated string is the e-mail address, you can split the string by spaces and use

filter_var($email, FILTER_VALIDATE_EMAIL)

on each substring.

mandaleeka
A: 

http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html

Do you really want to use a regexp? Just split by spaces.

liori