How to Extract Domain from Email in PHP

How to extract the domain name from an email address with PHP:

$email = "[email protected]";
 
// make sure we've got a valid email
if( filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
    // split on @ and return last value of array (the domain)
    $domain = array_pop(explode('@', $email));
 
    // output domain
    echo $domain
}

Post written by Ed Reckers

Founder and lead web development consultant at Red Bridge Internet : San Francisco WordPress Developers and Consultants.

2 Responses to “How to Extract Domain from Email in PHP”

  1. Christian

    Simple but clever 🙂

  2. Burak Erdem

    This gives the “Only variables should be passed by reference” error. You need to make the result of explode() a variable before you pass it on;

    $var =explode(‘@’, $email);
    $domain = array_pop($var);
    echo $domain;

Leave a Reply