Removing subdomains from a string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • akadeco
    New Member
    • Aug 2009
    • 7

    Removing subdomains from a string

    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.su bdomain.bytes.c om, 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
  • micmast
    New Member
    • Mar 2008
    • 144

    #2
    I would suggest explode the string and always take the last 2 values of the returned array.

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      To clarify micmast's suggestion, I believe he means split instead of explode.

      Code:
      >>> s = 'bytes.com'
      >>> '.'.join(s.split('.')[-2:])
      'bytes.com'
      >>> s = 'subsubdomain.subdomain.bytes.com'
      >>> '.'.join(s.split('.')[-2:])
      'bytes.com'
      >>>

      Comment

      • micmast
        New Member
        • Mar 2008
        • 144

        #4
        bvdet, yes you are right :) explode is php syntax.

        sorry.

        Comment

        Working...