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.

Leave a Comment