How to extract the domain name from an email address with PHP:
$email = "handle@domain.com"; // 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 } |
3 Responses to “How to Extract Domain from Email in PHP”
Christian
Simple but clever 🙂
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;
Richard Feliciano
$domain = explode(‘@’, $email)[1];