php email validation
intro
so here’s an extremely common task that i find myself doing all the time and thought id share. when building a simple sign up form there is usually a place for the singer upper to put an email address so they can receive whatever it is you have to offer. however, sometimes these forms are used to send you exorbitant amount of spam, or the user simply made a type when entering the address. either way the results are undesirable so you might want to validate the email address to make sure its legit,
the regexp
first things first, you have to make sure the email address is formatted properly. to do this you will want to compare the address to a regular expression for a match. php has a wonderful function called preg_match that will take care of this for you provided you know the regular expression to use, and i have it here for you:
/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/
this expression basically says “start with a letter or number; followed by any number of letters, numbers or periods; then an @ symbol; finish with one more more letters followed by at least one or more periods and/or letters”. to use this with php you simply assign this pattern to a variable and compare that variable against the address given:
$email = "someone@email.com";
$pattern = "/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/";
if (preg_match($pattern, $email)) {
do something here
} else {
$error = "invalid email";
}
notice i wrapped the preg_match() in an if statement. thats cause it returns a boolean true or false depending on whether a match is found or not. now we know the email address was formatted properly, this might be all you need but i prefer to take an additional step
checking the domain
checking the actual domain after the @ symbol can ensure that someone didn’t enter a properly formatted bogus address. first split the email address in half at the @ symbol then check the half after the the symbol to see if its legit:
list($username,$domain)=split('@',$email);
if(!checkdnsrr($domain,'MX')) {
$error = "invalid domain";
}
in this case im checking to see if the checkdnsrr failed, and setting an error message if it does.
put it together
putting all the code together will give you a bit of script that will check the syntax of the email address, and if its correct, check the dns records for the domain name. i prefer to do the checks and set and error message when an error is found. then, after the checks, if any errors were found, i can display them rather than complete the form submission:
$error = "";
$email = "someone@email.com";
$pattern = "/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/";
if (preg_match($pattern, $email)) {
list($username,$domain)=split('@',$email);
if(!checkdnsrr($domain,'MX')) {
$error = "invalid domain";
}
} else {
$error = "invalid email";
}
if (strlen($error)) {
echo "error: " . $error;
} else {
finish the submission of the form
}
converting text urls to clickable links
today i came across a challenge that i am surprise i have not faced before but it seemed worthy to make a post about. i have been working on a social networking tool for a company and they wanted all urls to automatically be parsed into clickable links regardless of the syntax (ie http://www.site.com, site.com, www.site.com). the solution was extremely simple: write a regular expression to match any url and use to make a comparison. heres the code:
<?php
$pattern = "@\b(https?://)?(([0-9a-zA-Z_!~*'().&=+$%-]+:)?[0-9a-zA-Z_!~*'().&=+$%-]+\@)?(([0-9]{1,3}\.){3}[0-9]{1,3}|([0-9a-zA-Z_!~*'()-]+\.)*([0-9a-zA-Z][0-9a-zA-Z-]{0,61})?[0-9a-zA-Z]\.[a-zA-Z]{2,6})(:[0-9]{1,4})?((/[0-9a-zA-Z_!~*'().;?:\@&=+$,%#-]+)*/?)@";
$new_text = preg_replace($pattern, '<a href="\0">\0</a>', $old_text);
?>
thats it, simple and effective. this is similar to how Facebook converts any url into a link in a status update. with the url in tack you could then use that url to capture a screenshop of the website and make it display along with the link.




