如何从当前URL中删除最后一个查询


How do I remove last query from current URL

假设我当前的url是

http://domain.com/category/clothing-personal-items/&page=3

我使用$_SERVER["REQUEST_URI"]来获取当前请求的uri。

$url = $_SERVER["REQUEST_URI"];
print_r(parse_url($url));
echo parse_url($url, PHP_URL_PATH);

阵列输出

Array
(
    [path] => /category/clothing-personal-items/&page=3
)

问题:

如何仅检索/category/clothing-personal-items/删除/category/clothing-personal-items/ 之后的任何查询

$url = explode("?", $url);
$url = array_shift($url);         // explode by ? if it is there
$url = explode("&", $url);
$url = array_shift($url);       // other wise explode by & and return the first item
$temp = $_GET;
array_pop($temp);
$new_query = http_build_query($temp);

然后只需将$new_query字符串添加到当前的$_SERVER['SCRIPT_NAME']

$parsed = parse_url($url, PHP_URL_PATH);
if(preg_match('/[^&?]+/',$parsed['path'],$matches))
    $path = $matches[0];
else
    echo "No path found";