从字符串获取基本URL

I have a simple script like this

$data = "http://example.com/index.php?data=data&value=value";

How to get base url from $data? The output will be like this

http://example.com

The output without this text " /index.php?data=data&value=value

Use parse_url()

$url = "http://example.com/index.php?data=data&value=value";
$url_info = parse_url($url);
echo $url_info['host'];//hostname

Additionally $user_info contain below

[scheme] => http
[host] => hostname
[user] => username
[pass] => password
[path] => /path
[query] => arg=value
[fragment] => anchor

SO you can get http by scheme

you can do something like :

echo $url_info['scheme'] . '://' . $url_info['host'];//http://example.com

Check Parsing Domain From URL In PHP

$url = 'http://google.com/dhasjkdas/sadsdds/sdda/sdads.html';
$parse = parse_url($url);
echo $parse['host']; // prints 'google.com'