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

时间:2012-02-13 18:44:35

标签: php

假设我当前的网址是

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/删除任何查询

3 个答案:

答案 0 :(得分:2)

$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

答案 1 :(得分:0)

$temp = $_GET;
array_pop($temp);

$new_query = http_build_query($temp);

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

答案 2 :(得分:0)

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