Get domain name from email using PHP

No comments
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():

$var = explode('@','');
echo $var[1];

By using substr() and strrchr(): You can extract domain name from email id by using php substr() and strrchr() functions.

echo substr(strrchr('', "@"), 1);

OR, we can do the same with javascript : If you want fetch domain from email id in javascript then below code is for you.

<script type="text/javascript">
var email = '';
var domain = email.replace(/.*@/, "");

No comments :

Post a Comment