Check if a string is an email address in PHP Check if a string is an email address in PHP php php

Check if a string is an email address in PHP


Without regular expressions:

<?php    if(filter_var("some@address.com", FILTER_VALIDATE_EMAIL)) {        // valid address    }    else {        // invalid address    }?>


if(filter_var($email, FILTER_VALIDATE_EMAIL)){    echo 'This is a valid email address.';    echo filter_var($email, FILTER_VALIDATE_EMAIL);    //exit("E-mail is not valid");}else{    echo 'Invalid email address.';} 


This is not a great method and doesn't check if the email exists but it checks if it looks like an email with the @ and domain extension.

function checkEmail($email) {   $find1 = strpos($email, '@');   $find2 = strpos($email, '.');   return ($find1 !== false && $find2 !== false && $find2 > $find1);}$email = 'name@email.com';if ( checkEmail($email) ) {   echo $email . ' looks like a valid email address.';}