How to get host name from this kind of URL?

牧云@^-^@ 提交于 2020-01-21 09:07:45

问题


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=243432432424243

How to take the http://hostname.com from the above URL using php?


回答1:


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).




回答2:


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


来源:https://stackoverflow.com/questions/4578636/how-to-get-host-name-from-this-kind-of-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!