Parsing domain from a URL

前端 未结 18 2475
独厮守ぢ
独厮守ぢ 2020-11-22 12:26

I need to build a function which parses the domain from a URL.

So, with

http://google.com/dhasjkdas/sadsdds/sdda/sdads.html

or

18条回答
  •  被撕碎了的回忆
    2020-11-22 12:49

    I'm adding this answer late since this is the answer that pops up most on Google...

    You can use PHP to...

    $url = "www.google.co.uk";
    $host = parse_url($url, PHP_URL_HOST);
    // $host == "www.google.co.uk"
    

    to grab the host but not the private domain to which the host refers. (Example www.google.co.uk is the host, but google.co.uk is the private domain)

    To grab the private domain, you must need know the list of public suffixes to which one can register a private domain. This list happens to be curated by Mozilla at https://publicsuffix.org/

    The below code works when an array of public suffixes has been created already. Simply call

    $domain = get_private_domain("www.google.co.uk");
    

    with the remaining code...

    // find some way to parse the above list of public suffix
    // then add them to a PHP array
    $suffix = [... all valid public suffix ...];
    
    function get_public_suffix($host) {
      $parts = split("\.", $host);
      while (count($parts) > 0) {
        if (is_public_suffix(join(".", $parts)))
          return join(".", $parts);
    
        array_shift($parts);
      }
    
      return false;
    }
    
    function is_public_suffix($host) {
      global $suffix;
      return isset($suffix[$host]);
    }
    
    function get_private_domain($host) {
      $public = get_public_suffix($host);
      $public_parts = split("\.", $public);
      $all_parts = split("\.", $host);
    
      $private = [];
    
      for ($x = 0; $x < count($public_parts); ++$x) 
        $private[] = array_pop($all_parts);
    
      if (count($all_parts) > 0)
        $private[] = array_pop($all_parts);
    
      return join(".", array_reverse($private));
    }
    

提交回复
热议问题