Dirty Bird Design
Dirty Bird Design

Reputation: 5523

PHP remove part of URL before querystring

I have url like this http://example.com/dir1/dir2/dir3/dir4/Page.php?charts.htm and I need to remove the page/extension before the querystring so it outputs http://example.com/dir1/dir2/dir3/dir4/?charts.htm

$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$temp = explode( '?'  , $url );
$temp[0] = ""; //remove part before the ?

Not even sure if I'm on the right track here

Upvotes: 0

Views: 71

Answers (2)

Jan
Jan

Reputation: 43169

You can use a combination of parse_url() and dirname()

$url = "http://example.com/dir1/dir2/dir3/dir4/Page.php?charts.htm";
$urlArray = parse_url($url); // Array ( [scheme] => http [host] => example.com [path] => /dir1/dir2/dir3/dir4/Page.php [query] => charts.htm )

$newURL  = $urlArray["scheme"]."://".$urlArray["host"]."/";
$newURL .= dirname($urlArray["path"])."/?".$urlArray["query"];
// gives http://example.com/dir1/dir2/dir3/dir4/?charts.htm

Upvotes: 1

barner_j
barner_j

Reputation: 51

try this :

$url = str_replace("Page.php", "", $url);

Upvotes: 1

Related Questions