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():
<?php $var = explode('@','[email protected]'); echo $var; ?>
By using substr() and strrchr(): You can extract domain name from email id by using php substr() and strrchr() functions.
<?php echo substr(strrchr('[email protected]', "@"), 1); ?>