Page 1 of 1

Can you check if an email is valid?

Posted: Fri Aug 02, 2013 5:19 am
by simonmlewis
Is there a script, or something in PHP that queries the input or output of an email address from a database, and checks to ensure it is correct?

ie. that there is @ in it, there are two -- in a line and so on.

The software someone is using may do this anyway, but would be cool to insert the bulk amount into PHP and it filters them all out.

Thanks

Simon

Re: Can you check if an email is valid?

Posted: Fri Aug 02, 2013 6:17 am
by pbs

Re: Can you check if an email is valid?

Posted: Fri Aug 02, 2013 6:21 am
by simonmlewis
Brilliant. Thanks. Very useful.
Can you add to it, or see how it filters them? As I was about to upload emails with double hyphens.

Re: Can you check if an email is valid?

Posted: Fri Aug 02, 2013 11:00 am
by AbraCadaver
FILTER_VALIDATE_EMAIL may still have some problems. It used a regex from an un-maintained PEAR package and I don't think was RFC compliant. It may be fixed in newest PHP versions I'm not sure. You can try this:

Code: Select all

function validate_email ($email) {
  //@see http://www.hm2k.com/posts/what-is-a-valid-email-address
  //@see http://hm2k.googlecode.com/svn/trunk/code/php/functions/validate_email.php
  $regex='/^[\w!#$%&\'*+\/=?^`{|}~.-]+@(?:[a-z\d][a-z\d-]*(?:\.[a-z\d][a-z\d-]*)?)+\.(?:[a-z]+)$/iD';
  
  if (preg_match($regex, $email, $m)) { return $m[0]; }
  return false;
}

Re: Can you check if an email is valid?

Posted: Fri Aug 02, 2013 11:05 am
by simonmlewis
Amateur question: how do I implenet that on a 'per extracted row from DB' basis?

I have used the filter method within a db extraction. Of over 312000 emails it found just 400 bad ones. Possible but unlikely to be accurate!

Re: Can you check if an email is valid?

Posted: Fri Aug 02, 2013 12:36 pm
by AbraCadaver
simonmlewis wrote:Amateur question: how do I implenet that on a 'per extracted row from DB' basis?

I have used the filter method within a db extraction. Of over 312000 emails it found just 400 bad ones. Possible but unlikely to be accurate!
I'm not completely sure what you're asking, but if you want to validate emails from db rows and optionally delete those rows, then something similar to this:

Code: Select all

$result = mysqli_query($link, "SELECT `id`, `email` FROM `table_name`");

while($row = mysqli_fetch_assoc($result)) {
    if(!validate_email($row['email'])) {
        $delete[] = $row['id'];
    }
}
$delete = implode(',', $delete);
$result = mysqli_query($link, "DELETE FROM `table_name` WHERE `id` IN ($delete)");

Re: Can you check if an email is valid?

Posted: Fri Aug 02, 2013 12:36 pm
by requinix
simonmlewis wrote:As I was about to upload emails with double hyphens.
Double hyphens are valid. Multiple consecutive periods aren't (strictly speaking).

For what it's worth, FILTER_VALIDATE_EMAIL checks using

Code: Select all

"/^(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){255,})(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){65,}@)(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22))(?:\\.(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-+[a-z0-9]+)*\\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-+[a-z0-9]+)*)|(?:\\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{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