【问题标题】:How to get host name from this kind of URL?如何从这种 URL 中获取主机名?
【发布时间】:2011-01-02 13:48:25
【问题描述】:

我想从内部 URL 获取主机名。如何处理。

eg :- 这是内部 url 格式.. http://hostname.com/folder/folder2/test.php?id=243432432424243

如何使用php从上述URL中获取http://hostname.com?

【问题讨论】:

    标签: php regex url hostname


    【解决方案1】:

    使用parse_url。

    您想要输出数组的 'scheme' 和 'host' 元素(可能还有 'port'、'user' 和 'password' - 如果需要,您可以使用 http_build_url 将这些位粘贴在一起) .

    【讨论】:

      【解决方案2】:

      一个简洁的方法是:

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

      这会产生:

      http://hostname.com
      

      【讨论】:

        猜你喜欢
        • 2012-03-26
        • 1970-01-01
        • 2021-08-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-14
        相关资源
        最近更新 更多