Jump to content


Photo

Seeing if a string contains an email address


  • Please log in to reply
2 replies to this topic

#1 JamieP

JamieP
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 24 March 2006 - 03:12 PM

Hi, I want to see if a string contains an email address, I know how to see if it JUST contains an email address, but I don't want that, it's for a private messaging system, and we do not allow email addresses to be sent, so I need a way to check to see if the variable containing the text contains an email address.

#2 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 24 March 2006 - 03:37 PM

[!--quoteo(post=357940:date=Mar 24 2006, 03:12 PM:name=JamieP)--][div class=\'quotetop\']QUOTE(JamieP @ Mar 24 2006, 03:12 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Hi, I want to see if a string contains an email address, I know how to see if it JUST contains an email address, but I don't want that, it's for a private messaging system, and we do not allow email addresses to be sent, so I need a way to check to see if the variable containing the text contains an email address.
[/quote]

ok i'm sure there's a shorter pattern than this, but try:

$email = "this is a string with an email: hello@example.com in it";

$result = preg_match("/[A-Za-z0-9][A-Za-z0-9\.\_]*[@][A-Za-z0-9\.]*[.]([A-Za-z]{2,3})[.]?([A-Za-z]{2})?/", $email);

if ($result)
   echo 'email found in string';
else
   echo 'no emails';

"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#3 JamieP

JamieP
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 24 March 2006 - 08:59 PM

Thanks redbullmarky, that worked =D




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users