domain regex split

前端 未结 4 1236
情深已故
情深已故 2021-01-27 08:20

I have some domains I want to split but can\'t figure out the regex...

I have:

  • http://www.google.com/tomato
  • http://int.google.c
4条回答
  •  情话喂你
    2021-01-27 09:02

    You can do this on a best bet basis. The last part of the URL is always the TLD (and optional root). And you are basically looking for any preceeding word that is longer than 2 letters:

    $url = "http://www.google.co.uk./search?q=..";
    
    preg_match("#http://
                (?:[^/]+\.)*       # cut off any preceeding www*
                ([\w-]{3,})        # main domain name
                (\.\w\w)?          # two-letter second level domain .co
                \.\w+\.?           # TLD
                (/|:|$)            # end regex with / or : or string end
                #x", 
          $url, $match);
    

    If you expect any longer second-level domains (.com maybe?) then add another \w. But this is not very generic, you would actually need a list for TLDs were this was allowed.

提交回复
热议问题