Posted inphp

How to extract domain name from email address in PHP?

Today, We want to share with you php get domain name.In this post we will show you how to extract domain name from email using PHP?, hear for extract username from email php we will give you demo and example for implement.In this post, we will learn about PHP Get Domain Name From Full URL With Parameters with an example.

Get only domain name from email in PHP

$prefix = substr($email, 0, strrpos($email, '@'));

Get email domains name without name before @ and tld

Example 1:

$email = "[email protected]";
$domain_name = substr(strrchr($email, "@"), 1);
echo "Domain name is :" . $domain_name;

Another way to extract domains name from email.

extract domain name from email

Example 2:

$email = "[email protected]"; 
$parts = explode("@",$email); 
$username = $parts[0]; 
echo $username;

split on @ and return last value of array (the domain)

HOW TO EXTRACT DOMAIN FROM EMAIL IN PHP?

Example 3:
Sometimes you need to extract domains name from email using PHP for whatever reason, in that case you can use source code below :

email = "[email protected]";


if( filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
    // split on @ and return last value of array (the domain)
    $domain = array_pop(explode('@', $email));

    echo $domain
}

I hope you get an idea about PHP String Exercises: Extract the user name from the specified email ID.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

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

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype