By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,757 Members | 1,990 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,757 IT Pros & Developers. It's quick & easy.

Validating a Paragraphed String

P: 1
Well I need a quick solution for checking if a string contains an email address or anything of the similar, example:

I allow user to type in their personal profile in a textarea and name it "profile"

so in php i will catch it in $profile but would want to check if there is an email value in it. If there is, an error will be trigered as this is not allowed.

Please help.

Thanks
May 19 '07 #1
Share this Question
Share on Google+
2 Replies


pbmods
Expert 5K+
P: 5,821
Heya, basher. Welcome to TSDN!

You'll be wanting to use preg_match, and you'll probably like this article on searching for email addresses.
May 19 '07 #2

Odisey
P: 14
Well I need a quick solution for checking if a string contains an email address or anything of the similar, example:

I allow user to type in their personal profile in a textarea and name it "profile"

so in php i will catch it in $profile but would want to check if there is an email value in it. If there is, an error will be trigered as this is not allowed.

Please help.

Thanks
Basher you want to use 'regular expressions'. Do a Google for recipe cut and paste scripts. Regular expressions in short are characters used - in this case - to constrain your data input to form. In other words, if a user does not input an email address as something@someplace.ext they will get the }else{ error you code.

Here is an example:

I am using the eregi() function as !eregi and the eregi_replace. What this does is first determines if the input is a formated email address. If not, the error. If so, it recodifies it as a link. YOU can adjust it of course. Learn more about Regular Expressions!

Expand|Select|Wrap|Line Numbers
  1.  
  2. //Check the mail address
  3.  
  4.    if (!eregi ('^[[:alnum:]][a-z0-9_\.\-]*@[a-z0-9\.\-]+\.[a-z]{2,4}$',
  5.    stripslashes(trim($_POST['email'])))) {
  6.       $problem = TRUE;
  7.       $message .= '<p class="error">Please enter a valid email address.</p>';
  8.  
  9.     }  else  {
  10.  
  11.     $email = eregi_replace ('^[[:alnum:]][a-z0-9_\.\-]*@[a-z0-9\.\-]+\.[a-z]{2,4}$', '<a href="mailto:\\0">Email</a>', 
  12.     stripslashes(trim($_POST['email'])));
  13.  
  14. }    
  15.  
  16.  
  17.  
  18.  
Marc
May 20 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.