# \[PHP\] - Extract domain without subdomains from email address

**URL:** <https://forum.kirupa.com/t/php-extract-domain-without-subdomains-from-email-address/196423>\
**Category:** Uncategorized\
**Created:** [August 8, 2006, 9:19am UTC](https://forum.kirupa.com/t/php-extract-domain-without-subdomains-from-email-address/196423 "2006-08-08T09:19:43Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![redrum87](https://avatars.discourse-cdn.com/v4/letter/r/f04885/32.png) [@redrum87](https://forum.kirupa.com/u/redrum87)\
**Post date:** [August 8, 2006, 9:19am UTC](https://forum.kirupa.com/t/php-extract-domain-without-subdomains-from-email-address/196423/1 "2006-08-08T09:19:43Z")

</div>

I need to grab the domain without any subdomains from a user inputed email address. For example:

[myname@domain.com](mailto:myname@domain.com) = [domain.com](http://domain.com)  
[myname@domain.co.uk](mailto:myname@domain.co.uk) = [domain.co.uk](http://domain.co.uk)

^ Those are easy because I can just take whatever comes after the @ sign. The problem comes in with sub domains…

[myname@sub1.domain.edu](mailto:myname@sub1.domain.edu) = domain.edu  
[myname@sub1.sub2.domain.co.uk](mailto:myname@sub1.sub2.domain.co.uk) = [domain.co.uk](http://domain.co.uk)

When I was in the planning stages for this, I thought it would be easy and could just be handled with an explode(), but, I don’t think it can… Here is my attempt:

```php
$_POST['email'] = trim($_POST['email']);
    $EmailExplodeAt = explode("@",$_POST['email'], 2);
    $EmailExplodeDomains = explode(".",$EmailExplodeAt);
    //can't figure out how to grab the domain and TLD only

```

I’m not sure if this can be done with just PHP or if it requires a regex. I’d prefer to do it with PHP only, simply because I don’t know how to write regular expressions yet. I searched Google quite a bit and couldn’t turn up anything. Any ideas?
