dongshan0202405 2015-03-10 09:19
浏览 76
已采纳

如何保留一些URL参数并删除所有其他参数

I have a URL with a range of parameters:

locations.php?use_url=on&zipcode=Axminster%2C+Devon+EX13+5RZ&categories%5B0%5D=2&radius=30&checkboxnational=1&submit=loading+results...&resultbar=25&sortbar=distance&lat=50.77643440000001&lng=-2.9810417&swlat=50.7773526197085&swlng=-2.9814507999999478&nelat=50.7800505802915&nelng=-2.9749947000000247&outcode=EX13

I am currently using the unset() command to remove all parameters except for some base parameters such as 'lat', 'lng', 'swlat, 'swlng', via the following code:

$url = $_SERVER["REQUEST_URI"];
$x = $url;
$parsed = parse_url($x);
$query = $parsed['query'];
parse_str($query, $params);
unset($params['outcode'],$params['checkboxnational']);
$string = http_build_query($params);

However, this morning I have realised that my unset() is looking way too long, because in reality I am trying to unset about 20 parameters and keep only about 5 parameters. And going forward I'll probably being adding more parameters to the URL, which increases the risk of errors being made. So what I'm now trying to do is write code that keeps these base parameters, and remove all other parameters without having to individually name all these other parameters. Is this possible?

I don't mind php or javascript, either way. As long as it gets the job done. I have searched high and low and all I can find is people wanting to remove certain parameters, not keep certain and remove all others. Many thanks.

  • 写回答

1条回答 默认 最新

  • douba9654 2015-03-10 09:32
    关注

    If parameters you want are always the same (I mean, keys are the same), then just do this:

    $myWantedParams = array('first' => $params['first'], 'second' => $params['second'] ... );
    unset($params);
    

    Instead of thinking how to remove parameters, just focus on what you really need and take it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)