Parsing domain from a URL

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

    Combining the answers of worldofjr and Alix Axel into one small function that will handle most use-cases:

    function get_url_hostname($url) {
    
        $parse = parse_url($url);
        return str_ireplace('www.', '', $parse['host']);
    
    }
    
    get_url_hostname('http://www.google.com/example/path/file.html'); // google.com
    

提交回复
热议问题