How to sanitize and validate an email address with filter in PHP

2 Answers

0 votes
/*
mixed filter_var ( mixed $variable [, int $filter = FILTER_DEFAULT [, mixed $options ]] )
*/
   
$email = "avi@collectivesolver.com";
 
// Remove illegal characters 
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
 
// Validate email
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) 
    echo("$email is a valid email address");
else 
    echo("$email is not a valid email address");
 
        
/*
run:
    
avi@collectivesolver.com is a valid email address  
  
*/

 



answered Dec 25, 2015 by avibootz
edited Dec 26, 2015 by avibootz
0 votes
/*
mixed filter_var ( mixed $variable [, int $filter = FILTER_DEFAULT [, mixed $options ]] )
*/
  
$email = "avi@collectivesolver.com.net.org";

// Remove illegal characters 
$email = filter_var($email, FILTER_SANITIZE_EMAIL);

// Validate email
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) 
    echo("$email is a valid email address");
else 
    echo("$email is not a valid email address");
 
       
/*
run:
   
avi@@collectivesolver.com is not a valid email address 
 
*/

 



answered Dec 25, 2015 by avibootz
edited Dec 26, 2015 by avibootz

Related questions

2 answers 342 views
3 answers 353 views
353 views asked Jun 18, 2016 by avibootz
2 answers 259 views
1 answer 181 views
1 answer 211 views
...