By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,369 Members | 1,138 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,369 IT Pros & Developers. It's quick & easy.

Removing subdomains from a string

P: 7
Hi

Could anyone give me some pointers on how to remove subdomains from a string?

For example:
  • If the string was bytes.com, it would stay bytes.com
  • If it was subdomain.bytes.com, it would become bytes.com
  • If it was subsubdomain.subdomain.bytes.com, it would become bytes.com

Essentially, it just needs to strip off the subdomains and produce the root domain if not already present.

Cheers
akadeco
Sep 11 '09 #1
Share this Question
Share on Google+
3 Replies


micmast
100+
P: 144
I would suggest explode the string and always take the last 2 values of the returned array.
Sep 11 '09 #2

bvdet
Expert Mod 2.5K+
P: 2,851
To clarify micmast's suggestion, I believe he means split instead of explode.

Expand|Select|Wrap|Line Numbers
  1. >>> s = 'bytes.com'
  2. >>> '.'.join(s.split('.')[-2:])
  3. 'bytes.com'
  4. >>> s = 'subsubdomain.subdomain.bytes.com'
  5. >>> '.'.join(s.split('.')[-2:])
  6. 'bytes.com'
  7. >>> 
Sep 11 '09 #3

micmast
100+
P: 144
bvdet, yes you are right :) explode is php syntax.

sorry.
Sep 11 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.