All about PHP/MySQL

Thursday, February 5, 2009

Clean An Email Address

Use this function to cleanup an email address. It will remove any unwanted character smoothly.

Function:

function clean_email($email = "")
{
$email = trim($email);
$email = str_replace(" ", "", $email);

// Check for more than one @
if (substr_count($email, '@') > 1)
{
return false;
}

$email = preg_replace("#[\;\#\n\r\*\'\"<>&\%\!\(\)\{\}\[\]\?\\/\s]#", "", $email);

if (preg_match("/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,4})(\]?)$/", $email))
{
return $email;
}
else
{
return false;
}
}

Usage:

$email_address = clean_email("my!email@domain.com");

It will save "myemail@domain.com" after removing the typo mistake (probably) by user.

Happy Coding :)

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]



Links to this post:

Create a Link

<< Home