0

I want to work around email addresses and I want to explode them using php's explode function.

It's ok to separate the user from the domain or the host doing like this:

list( $user, $domain ) = explode( '@', $email );

but when trying to explode the domain to domain_name and domain_extention I realised that when exploding them using the "." as the argument it will not always be foo.bar, it can sometimes be foo.ba.ar like fooooo.co.uk

so how to separate "fooooo.co" from "uk" and let the co with the fooooo. so finally I will get the TLD separated from the other part.

I know that co.uk is supposed to be treated as the TLD but it's not official, like fooooo.nat.tn or fooooo.gov.tn

Thank You.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
medk
  • 9,233
  • 18
  • 57
  • 79

4 Answers4

2

Just use strripos() to find the last occurrence of ".":

$blah = "hello.co.uk";

$i = strripos($blah, ".");

echo "name = " . substr($blah, 0, $i) . "\n";
echo "TLD = " . substr($blah, $i + 1) . "\n";
NullUserException
  • 83,810
  • 28
  • 209
  • 234
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
1

Better use imap_rfc822_parse_adrlist or mailparse_rfc822_parse_addresses to parse the email address if available. And for removing the “public suffix” from the domain name, see my answer to Remove domain extension.

Community
  • 1
  • 1
Gumbo
  • 643,351
  • 109
  • 780
  • 844
1

Expanding on Oli's answer...

substr($address, (strripos($address, '.') + 1));

Will give the TLD without the '.'. Lose the +1 and you get the dot, too.

xpda
  • 15,585
  • 8
  • 51
  • 82
Surreal Dreams
  • 26,055
  • 3
  • 46
  • 61
0

end(explode('.', $email)); will give you the TLD. To get the domain name without that, you can do any number of other string manipulation tricks, such as subtracting off that length.

Reese Moore
  • 11,524
  • 3
  • 24
  • 32
  • thank you sir but this gave me a wrong result: when I used fooooo@ymail.com.tn, this gave me t as the firs result and n as the second result it separated .tn to t n – medk Nov 07 '10 at 19:42