[eluser]Kyle Johnson[/eluser]
Hey Everyone,
I'm not sure if I posted this before, but I love refinding fun bits of code as you're diving through and making changes to how things are organized. Parts of it were written by me, parts from this article here (
http://www.linuxjournal.com/article/9585).
I find it super useful for validating user input:
Code:
/**
* Returns an array of validated email addresses.
* @param mixed $email Email addresses to be checked, can be an array of arrays, mixed separators, anything.
* @return array An array of valid email addresses based on the data sent to the function.
*/
function parse_email($email = array()) {
$temp = array();
$emailArr = array();
if (is_array($email)) {
foreach ($email as $item) {
$temp = array_merge(parse_email($item), $temp); //recursion
}
} else {
$mainDelimeter = ","; //replace all delimiters with this one
$delimeters = array(";", " ", ","); //valid delimeters to split apart
foreach ($delimeters as $delimiter) {
$email = str_replace($delimiter, $mainDelimeter, $email); //replace all delimiters with main delimiter
}
$temp = explode($mainDelimeter, $email); // create array of emails
}
for ($index = 0; $index < count($temp); $index++) {
$temp[$index] = trim($temp[$index]); //trim whitespace
if (strlen($temp[$index]) > 0) { // only look at email if len > 0
if (valid_email($temp[$index])) { // check that email address is valid, optionally can verify DNS name
$emailArr[] = $temp[$index]; // add to returned email list
} else {
// $emailArr[] = "INVALID EMAIL: {$temp[$index]}";
}
}
}
$emailArr = array_unique($emailArr); // remove duplicates
sort($emailArr); // sort alphabetically
return $emailArr;
}
/**
Validate an email address.
Provide email address (raw input)
Returns true if the email address has the email
address format and the domain exists.
http://www.linuxjournal.com/article/9585
http://www.ilovejackdaniels.com/php/email-address-validation
*/
function valid_email($email, $checkDNS = FALSE) {
$isValid = true;
$atIndex = strrpos($email, "@");
if (is_bool($atIndex) && !$atIndex) {
$isValid = false;
} else {
$domain = substr($email, $atIndex + 1);
$local = substr($email, 0, $atIndex);
$localLen = strlen($local);
$domainLen = strlen($domain);
if ($localLen < 1 || $localLen > 64) {
// local part length exceeded
$isValid = false;
} else if ($domainLen < 1 || $domainLen > 255) {
// domain part length exceeded
$isValid = false;
} else if ($local[0] == '.' || $local[$localLen - 1] == '.') {
// local part starts or ends with '.'
$isValid = false;
} else if (preg_match('/\\.\\./', $local)) {
// local part has two consecutive dots
$isValid = false;
} else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)) {
// character not valid in domain part
$isValid = false;
} else if (preg_match('/\\.\\./', $domain)) {
// domain part has two consecutive dots
$isValid = false;
} else if
(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
str_replace("\\\\", "", $local))) {
// character not valid in local part unless
// local part is quoted
if (!preg_match('/^"(\\\\"|[^"])+"$/',
str_replace("\\\\", "", $local))) {
$isValid = false;
}
}
if ($checkDNS == TRUE) {
if ($isValid && !(checkdnsrr($domain, "MX") || checkdnsrr($domain, "A"))) {
// domain not found in DNS
$isValid = false;
}
}
}
return $isValid;
}
Usage:
Output: