假设我当前的网址是
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/
或删除任何查询
答案 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";