How to Check if String is Email Address in PHP?

03-Apr-2023

.

Admin

How to Check if String is Email Address in PHP?

Hi Dev,

Today, I will let you know example of how to check if string is email address in php?. you'll learn how validate email in php explain with example?. it's simple example of do you check if a string is an email address php?. I would like to share with you php validation email: check if the email address.

There are example to php check if string is an email in php. in this example, we will use to preg_match() function to check if string is an email in php . so let's the following example with output.

Example 1:


index.php

<?php

function checkemail($str) {

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

}

if(!checkemail("vivekpatel4950@gmail.com")){

echo "Invalid email address.";

}

else{

echo "Valid email address.";

}

?>

Output:

Valid email address

Example 2:

index.php

<?php

function checkemail($str) {

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

}

if(!checkemail("vivekpatel4950gmail.com")){

echo "Invalid email address.";

}

else{

echo "Valid email address.";

}

?>

Output:

Invalid email address

I hope it could help you...

#PHP