Get domain name from email using PHP

By | August 8, 2020

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('@','[email protected]');
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('[email protected]', "@"), 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 = '[email protected]';
var domain = email.replace(/.*@/, "");

Here is a video tutorial:

Category: Uncategorized

Leave a Reply

Your email address will not be published. Required fields are marked *