Parsing domain from a URL

前端 未结 18 2495
独厮守ぢ
独厮守ぢ 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:28

    This will generally work very well if the input URL is not total junk. It removes the subdomain.

    $host = parse_url( $Row->url, PHP_URL_HOST );
    $parts = explode( '.', $host );
    $parts = array_reverse( $parts );
    $domain = $parts[1].'.'.$parts[0];
    

    Example

    Input: http://www2.website.com:8080/some/file/structure?some=parameters

    Output: website.com

提交回复
热议问题