Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get base url from string

Tags:

php

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

like image 697
G.I Joe Avatar asked Jul 15 '26 05:07

G.I Joe


1 Answers

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

like image 79
Devsi Odedra Avatar answered Jul 22 '26 15:07

Devsi Odedra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!