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?

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

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 = '[email protected]';
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..

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.