减去一个$ _GET变量

时间:2013-10-19 23:23:04

标签: php

我当前的代码

<?php echo $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; ?>

返回:localhost / jamie / dashboard.php?p = 1

但是会有返回的情况:localhost / jamie / dashboard.php?p = 1&amp; sort = name

以及:localhost / jamie / dashboard.php?view = list&amp; p = 1

如何删除字符串中的p变量?我希望为同一页面上链接的$ _GET ['p']使用不同的值

<?php echo $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'] . '?p=' . $cpage + 1; ?>

返回:localhost / jamie / 1

2 个答案:

答案 0 :(得分:4)

您可以使用$_SERVER['SCRIPT_NAME']来获取dashboard.php位。或者可能更好用 parse_url获取您需要的网址部分,例如:

$url = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$parts = parse_url($url);
$params = $_GET;
$params['p'] = $cpage + 1;
echo $_SERVER['SERVER_NAME'] . $parts['path'] . '?' . http_build_query($params);

答案 1 :(得分:0)

尝试在括号中添加$ cpage + 1:<?php echo $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'] . '?p=' . ($cpage + 1); ?>