Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a string is an email address in PHP

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"); 
like image 584
homework Avatar asked Nov 12 '09 22:11

homework


People also ask

How do you check if a string is an email address?

To verify that the email address is valid, the IsValidEmail method calls the Regex. Replace(String, String, MatchEvaluator) method with the (@)(. +)$ regular expression pattern to separate the domain name from the email address.

Which are the two different ways to validate email addresses in PHP?

Explanation: In the above example, passing the input email address to the predefined function filter_var(), which takes two parameters as input email and second is type of email filter. This function filters the email and returns true or false. Method 3: Email validation using FILTER_SANITIZE_EMAIL filter.


2 Answers

Without regular expressions:

<?php     if(filter_var("[email protected]", FILTER_VALIDATE_EMAIL)) {         // valid address     }     else {         // invalid address     } ?> 
like image 199
Uri Avatar answered Sep 16 '22 15:09

Uri


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 = '[email protected]'; if ( checkEmail($email) ) {    echo $email . ' looks like a valid email address.'; } 
like image 25
TURTLE Avatar answered Sep 17 '22 15:09

TURTLE