Check if a string is an email address in PHP

homework picture homework · Nov 12, 2009 · Viewed 88.5k times · Source

I am trying to do an SQL query, but I need to check somehow if the value is an email address. I need a way to check if $user is an email address, because I have user values such as this in my table.

test
test2
[email protected]
[email protected]
test392
[email protected]

and so on...

I need to make it so $useremail checks $user to find if it's an email address. So I can UPDATE the values, WHERE user=test OR [email protected], etc.

$user = strtolower($olduser);
$useremail = "";

mysql_query("UPDATE _$setprofile SET user=$sn, fc=$fc WHERE user='$user' OR user='$useremail");

Answer

Uri picture Uri · Nov 12, 2009

Without regular expressions:

<?php
    if(filter_var("[email protected]", FILTER_VALIDATE_EMAIL)) {
        // valid address
    }
    else {
        // invalid address
    }
?>