Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php - strip path from url - no built in function?

Tags:

php

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?

like image 367
Marty Wallace Avatar asked Oct 02 '22 20:10

Marty Wallace


1 Answers

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);
like image 70
Ben Avatar answered Oct 23 '22 18:10

Ben