我有这个 URI。
我正在使用以下代码获取 URI 的基本名称。
$basename = basename($_SERVER['REQUEST_URI']);
上面的代码给了我下面的字符串。
index.php?properties&status=av&page=1
我想从字符串中删除最后一个变量,即 &page=1
。请注意 page
的值并不总是 1
。牢记这一点,我想以这种方式修剪变量。
Trim from the last position of the string till the first delimiter i.e
&
更新:
我想从字符串中删除 &page=1
,无论它在哪个位置。
我该怎么做?
最佳答案
与其使用正则表达式,不如将字符串解析为 url(它是什么)
$string = 'index.php?properties&status=av&page=1';
$parts = parse_url($string);
$queryParams = array();
parse_str($parts['query'], $queryParams);
现在只需删除参数
unset($queryParams['page']);
并重建 url
$queryString = http_build_query($queryParams);
$url = $parts['path'] . '?' . $queryString;
关于php - 使用php删除GET变量的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7195342/