Skip to content

Instantly share code, notes, and snippets.

@johnabela
Last active September 12, 2017 07:46
Show Gist options
  • Save johnabela/9a41191e9946ed59addae447b3c72206 to your computer and use it in GitHub Desktop.
Save johnabela/9a41191e9946ed59addae447b3c72206 to your computer and use it in GitHub Desktop.
Sometimes simple is the best
/*
We live in a world where internationalization is becoming more and more important for website developers to be conscience of.
RTF standards are a mess, we can all agree on that.
Attempting to validate email addresses, based on RTF standards, is simply impossible if you want to accept non utf-8 formatted email addresses.
Never trust the browser to properly handle utf-8 parsing.
Never trust php to properly handle utf-8 parsing, it is usually worse than the big three browsers.
This page has a number of valid email addresses, which I have used in this code example:
https://en.wikipedia.org/wiki/International_email#Email_addresses
You can regex until your eyes go blind and never find a way to properly RTF validate these intl email addresses.
The below method is one of those "stupid simple" methods of validation.
It checks for three things:
1) does the string have an at (@) symbol?
2) does the string have a period (.) symbol?
3) does the string have any text after the last period(.), to make sure a tld exists?
Any other type of validation, especially for intl email addresses is probably going to fail, or likely will in the future, especially as the world of TLD's becomes broader.
[first IF check] So why not take the simple approach, and just validate for the two parts of every email address that has to exist... an @ and a .
[second IF check] After that, make sure that the @ and the . are NOT right next to each other, as that too will never be a valid email address.
After these to checks, do the proper thing, and during account registration send an email confirmation to the email address and only activate the account if the user authenticates the email address confirmation.
So yeah, enough with all the stupidly complicated attempts to regular expression / RTF validate email addresses, and just go back to the simple basics.
*/
// the last email address in this array should fail the second IF check
$array = ['foo@bar.com','θσερ@εχαμπλε.ψομ','用户@例子.广告','उपयोगकर्ता@उदाहरण.कॉम','юзер@екзампл.ком','Dörte@Sörensen.example.com','аджай@экзампл.рус','foo@.net'];
foreach ($array as $email) {
if ((strpos($email,'@') === false) || (strpos($email,'.') === false)) {
die('invalid email address: ' . $email);
}
if (!trim(strstr(strstr($email,'@'),'.',true),'@.')) {
die('invalid email: ' . $email);
}
// logic:
// substr($email, strrpos($email, '@')+1) == get the last @, because $email = '"foo@bar"@foobar.com'; is a legit email
// explode('.', ... )[1] -- returns anything after the last .
if ( !explode('.', substr($email, strrpos($email, '@')+1))[1] ) {
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment