Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnabela/e296071ebc039d5a5b30b27c95b3a5f9 to your computer and use it in GitHub Desktop.
Save johnabela/e296071ebc039d5a5b30b27c95b3a5f9 to your computer and use it in GitHub Desktop.
A more proper way to get the domain.tld from an email address in php
// too many php developers use the example on the php `strstr` page:
// http://php.net/manual/en/function.strstr.php
// Yet, amazingly, the following is a valid email address:
// foo@bar@domain.tld
// If you use the example on the strstr page you end up with:
// @bar@domain.tld -- which is of course not what you want.
// So what is a fast and extremely low memory method to return
// just the domain.tld when? The following seems to be the best.
$email = 'foo@bar@domain.tld';
$domain = substr(strrchr($email, "@"), 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment