PHP is looking for a string for an email address

Hi, I am trying to find a string to see if it contains an email address and then will return it.

A typical vaildator email expression is:

eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email);

However, how would you search if it is in a string, for example, return the email address in a string:

"Hi, my name is Joe, you can contact us at joe@mysite.com. I'm also on Twitter."

I am a little puzzled, I know that I can search if it exists at all with \ b, but how can I return what I found.

Thanks.

+5
source share
3 answers

add $ regs as the last argument:

eregi("...", $email, $regs);
+4
source

preg_match(), .

$content = "Hi my name is Joe, I can be contacted at joe@mysite.com. I am also on Twitter.";
preg_match("/[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})/i", $content, $matches);

print $matches[0]; // joe@mysite.com
+9

Better PCRE to extract ADDR_SPEC:

 /[a-z0-9\._%+!$&*=^|~#%'`?{}/\-]+@([a-z0-9\-]+\.){1,}([a-z]{2,6})/

But if you really want to extract RFC 2822, you need something like:

 /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/

FROM.

0
source

All Articles