PHP filter_var, where have you been all my life?
- January 17th, 2009
- By David
- Write comment
Stumbled on something handy that I can’t believe I’m just now finding. Gotta love PHP convenience, including this built-in way to validate form input.
Thanks again, NETTUTS (tutorial here).
if (isset($_POST['email'])) {
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "$email is a valid email address.";
} else {
echo "$email is NOT a valid email address.";
}
}
// also FILTER_SANITIZE_URL, FILTER_VALIDATE_URL
