dongshanfan1941 2015-11-16 08:51
浏览 91
已采纳

从给定的URL获取主机名

how to get host name from bellow Example.

I/P: https://stackoverflow.com/users/login | O/P: stackoverflow.com

I/P: stackoverflow.com/users/login | O/P: stackoverflow.com

I/P: /users/login | O/P: (return empty string)

I checked parse_url function, but doesn't return what I need. Since, I'm beginner in PHP, it was difficult for me. If you have any idea, please answer.

  • 写回答

4条回答 默认 最新

  • dsadasd12132 2015-11-16 09:18
    关注

    you can try this

    <?php  
    
    function getHost($Address) { 
       $parseUrl = parse_url(trim($Address)); 
       return trim(isset($parseUrl['host']) ? $parseUrl['host'] : array_shift(explode('/', $parseUrl['path'], 2))); 
    } 
    
    echo getHost('http://stackoverflow.com/users/login');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?