PHP REGEX: Get domain from URL

前端 未结 9 1791
情歌与酒
情歌与酒 2020-12-08 11:05

What I want


I want to get from a URL the domain part so from http://example.com/ -> example.com

相关标签:
9条回答
  • 2020-12-08 11:31

    Assumes that http:// prefixes everything.

    $tmp = explode("/", $url);
    $domain = $tmp[2];
    
    0 讨论(0)
  • 2020-12-08 11:32
    $tmp = parse_url($url);
    $url = $tmp['host']
    
    0 讨论(0)
  • 2020-12-08 11:33

    This is like the regex from theraccoonbear but with support for HTTPS domains.

    if (preg_match('/https?:\/\/([^\/]+)\//i', $target_string, $matches)) {
      $domain = $matches[1];
    }
    
    0 讨论(0)
提交回复
热议问题