jim smith
jim smith

Reputation: 2454

get whole email address from @ symbol PHP Regex

The problem with this method is I don't get anything after .

$string = "this is a [email protected] email address";

preg_match('/\b(\w*@\w*)\b/', $string, $matches);

The result of $matches is

Array
(
    [0] => email@test

)

Is it possible to alter the regex to ignore them?

Upvotes: 0

Views: 38

Answers (2)

tenub
tenub

Reputation: 3446

. is not a part of \w. Alter your RegEx to something like: \b([\w.-]+@[\w.-]+)\b.

Upvotes: 0

p.s.w.g
p.s.w.g

Reputation: 149000

Try using a character class ([...]). Also you probably want to use a one-or-more quantifier (+):

preg_match('/\b([\w.]+@[\w.]+)\b/', $string, $matches);

You can include any other characters (such as - or +) you'd like to match as well.

Upvotes: 2

Related Questions