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
Can you check if an email is valid?
Moderator: General Moderators
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Can you check if an email is valid?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: Can you check if an email is valid?
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.
Can you add to it, or see how it filters them? As I was about to upload emails with double hyphens.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: Can you check if an email is valid?
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;
}mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: Can you check if an email is valid?
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 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!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: Can you check if an email is valid?
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: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!
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)");
Last edited by AbraCadaver on Fri Aug 02, 2013 12:37 pm, edited 1 time in total.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Re: Can you check if an email is valid?
Double hyphens are valid. Multiple consecutive periods aren't (strictly speaking).simonmlewis wrote:As I was about to upload emails with double hyphens.
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\\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