Validating URLs and Email Addresses in PHP

A few years ago, I posted a couple functions for validating email addresses using PHP. This is a follow up post that provides a simpler method for validating both email addresses and URLs.

Using PHP's filter_var() function, it's actually quite easy to do now—no messy regular expressions involved. The following wrapper functions force a true boolean response, so you can use them safely in your logic.

Email Addresses

function is_email($email) {
    return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
}

URLs

function is_url($url) {
    return filter_var($url, FILTER_VALIDATE_URL) !== false;
}

If you choose to not use the wrappers, just remember that filter_var() doesn't always return a boolean value. It will return the original string on success, and false on failure..

Author avatar

About the author

New Hampshirite building web apps in Florida. Creator of Surreal CMS, Postleaf, and DirtyMarkup.

Need to get in touch? Catch me on Twitter.