Jump to content

preg_replace for submitting email into MySQL database?


jdowen2211

Recommended Posts

I want to add an email option to a HTML that parses with PHP and submits data to a MySQL database but I'm not too sure are submitting email. I have the email field already created in the structure of the database but when the form is submitted, I want to make sure the email is valid. I use this for other $row's:

 

$firstname = preg_replace('#[^A-Za-z]#i', '', $_POST['firstname']); // filter everything but desired characters

 

So what preg_replace would I use for email?

Link to comment
Share on other sites

to validate that email address is real, use this simple function

function myCheckDNSRR($hostName, $recType = '') <BR>   
{ <BR>   
  if(!empty($hostName)) { <BR>   
    if( $recType == '' ) $recType = "MX"; <BR>   
    exec("nslookup -type=$recType $hostName", $result); <BR>   
    // check each line to find the one that starts with the host <BR>   
    // name. If it exists then the function succeeded. <BR>   
    foreach ($result as $line) { <BR>   
      if(eregi("^$hostName",$line)) { <BR>   
        return true; <BR>   
      } <BR>   
    } <BR>   
    // otherwise there was no mail handler for the domain <BR>   
    return false; <BR>   
  } <BR>   
  return false; <BR>   
}

Don't get it?

go here http://www.sitepoint.com/users-email-address-php/  ;D

Link to comment
Share on other sites

well you wouldn't really want to use preg_replace on an email...as it can strip characters that will make the email useless...however if a user includes unwanted characters invalid to an email address, the email address is obviously invalid anyway...heres a pattern to be used with preg_match

 

$pattern = '~[a-zA-Z0-9-_.]+@[a-zA-Z]+\.[a-z]{2,4}~i';
$subject = 'example.test@example.com';
if(preg_match($pattern,$subject)){ //if TRUE, a match has been found
      print "valid email";
}else{
      print "invalid email";
}

 

now there are a couple ways to limit the extension and host, however this is a simple and fairly secure pattern to use..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.