Get domain name from email using PHP
Last Modified at 2019-06-25 21:29:27
Recently, I was developing a project for my client, there was a specific requirement of client that user should not opt in with the email id that has been created from free email service providers. In that case, I needed to identify the domain name. In some other cases, for example: while developing email subscription form, we needed to identify that only self hosted email addresses should be entered by the customers, so we had to fetch domain name from email address so that we could validate if the domain of provided email id is not from free email service providers.
And here is the code that i created to get the domain name from email id:By using explode():
By using substr() and strrchr():
$var = explode('@','firstname.lastname@example.org');
You can extract domain name from email id by using php substr() and strrchr() functions.
echo substr(strrchr('email@example.com', "@"), 1);
var email = 'firstname.lastname@example.org';
var domain = email.replace(/.*@/, "");