How to get host name from this kind of URL?

前端 未结 2 423
我寻月下人不归
我寻月下人不归 2021-01-15 14:18

I want to get host name from and inner URL. how to take that.

eg :- this is the inner url format .. http://hostname.com/folder/folder2/test.php?id=243432432424

相关标签:
2条回答
  • 2021-01-15 14:44

    Use parse_url.

    You want the 'scheme' and 'host' elements of the output array (and maybe 'port', 'user' and 'password' as well - you can use http_build_url to stick these bits together if you want).

    0 讨论(0)
  • 2021-01-15 14:46

    A concise way would be:

    $url = 'http://hostname.com/folder/folder2/test.php?id=243432432424243';
    echo parse_url($url, PHP_URL_SCHEME).'://'.parse_url($url, PHP_URL_HOST);
    

    This produces:

    http://hostname.com
    
    0 讨论(0)
提交回复
热议问题