PHP has different ways of email validation, and one of the most popular ways is to achieve this task by using a regular expression.



This tutorial contains a PHP function to validate an email address. The function checks the input matching with patterns using regular expressions. PHP preg_match function has been used to search string for a pattern, and PHP ternary operator has been used to return the true or false value based on the pre_match return.

<?php

function valid_email($str) {
        return (!preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
}

if(!valid_email("[email protected]")){
        echo "Invalid email address.";
}else{
        echo "Valid email address.";
}

?>


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram