PHP在查询字符串之前删除部分URL

时间:2015-11-18 17:03:11

标签: php

我有这样的网址http://example.com/dir1/dir2/dir3/dir4/Page.php?charts.htm,我需要在查询字符串之前移除页面/扩展名,以便输出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 ?

甚至不确定我是否在正确的轨道上

2 个答案:

答案 0 :(得分:1)

试试这个:

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

答案 1 :(得分:1)

您可以使用parse_url()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