Given a URL:
http://mysite.com/this-is/the/path
I just want the scheme and the host I.e. http://mysite.com/
Currently I am using parse_url
and then building it back up like so:
$urlParts = parse_url($url);
$newUrl = $urlParts['scheme'] . "://" . $urlParts['host'] . "/";
Is there no PHP function that can strip the path off?
Not that I am aware of. This is a good example of a time where you may consider writing your own public function.
public function stripUrlPath($url){
$urlParts = parse_url($url);
$newUrl = $urlParts['scheme'] . "://" . $urlParts['host'] . "/";
return $newUrl;
}
Then use it throughout your code:
$newUrl = stripUrlPath($oldUrl);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With