How to get host name from this kind of URL?

2020-03-30 00:14发布

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?

2条回答
【Aperson】
2楼-- · 2020-03-30 00:47

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
查看更多
smile是对你的礼貌
3楼-- · 2020-03-30 00:53

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

查看更多
登录 后发表回答