dongmengan8620 2010-11-20 16:05
浏览 61
已采纳

从查询字符串中删除值

I have a URL in which a querystring is produced by a PHP script. Various values are displayed in the querystring.

Basically, I need to remove a specific value from the query string when a visitor clicks on a link or a 'remove' button.

So, the querystring looks like this:

http://www.foo.com/script.php?bar1=green&bar2=blue

But when a link or 'remove' button is clicked by a user, bar1=green is removed, and the visitor is directed to the following URL:

http://www.foo.com/script.php?bar2=blue

I thought this would be easy using basic HTML with a form or anchor but I haven't been able to do it so far.

Just so you know, i do not have access to the code on the PHP script itself; it is hosted remotely and is called to my webpage by a PHP wrapper using an iframe.

Any suggestions greatly appreciated.

Many thanks,

Matt

  • 写回答

2条回答 默认 最新

  • dsz1966 2010-11-20 16:31
    关注

    You can remove the value from the query string using this code:

    <?php
    function parseQueryString($url,$remove) {
        $infos=parse_url($url);
        $str=$infos["query"];
        $op = array();
        $pairs = explode("&", $str);
        foreach ($pairs as $pair) {
           list($k, $v) = array_map("urldecode", explode("=", $pair));
            $op[$k] = $v;
        }
        if(isset($op[$remove])){
            unset($op[$remove]);
        }
    
        return str_replace($str,http_build_query($op),$url);
    
    } 
    echo parseQueryString( "http://www.foo.com/script.php?bar1=green&bar2=blue","bar2");
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?