【问题标题】:php; remove single variable value pair from querystringphp;从查询字符串中删除单个变量值对
【发布时间】:2013-06-15 10:20:01
【问题描述】:

我有一个页面,它根据众多参数(即带值的变量)列出项目。

listitems.php?color=green&size=small&cat=pants&pagenum=1 etc.

为了能够编辑列表,我有一个参数 edit=1 附加到上面的查询字符串中:

listitems.php?color=green&size=small&cat=pants&pagenum=1&edit=1

到目前为止一切顺利。

当用户完成编辑时,我有一个退出编辑模式的链接。我希望这个链接指定整个查询字符串——不管它是什么,因为这取决于用户的选择——除了删除 edit=1。

当我只有几个变量时,我只是在链接中手动列出它们,但现在有更多,我希望能够以编程方式删除 edit=1。

我是否应该对 edit=1 进行某种搜索,然后将其替换为空?

$qs = str_replace("&edit=1, "", $_SERVER['QUERY_STRING']);
<a href='{$_SERVER['PHP_SELF']}?{$qs}'>return</a>;

或者什么是最干净最无错误的方法。

注意:在从一个页面转到另一个页面时,我遇到了类似的情况,我想取出 pagenum 并用不同的页面替换它。在那里,由于 pagenum 不同,我不能只搜索 pagenum=1 而是必须搜索 pagenum =$pagenum 如果这有任何区别。

谢谢。

【问题讨论】:

    标签: php


    【解决方案1】:

    我会使用http_build_query,它可以很好地接受参数数组并正确格式化它。您可以从$_GET 取消设置编辑参数并将其余部分推送到此函数中。

    请注意,您的代码缺少对 htmlspecialchars() 的调用。 URL 可以包含在 HTML 中有效的字符。因此,将其输出到链接时:Escape!

    一些例子:

    unset($_GET['edit']); // delete edit parameter;
    $_GET['pagenum'] = 5; // change page number
    $qs = http_build_query($_GET);
    
    ... output link here.
    

    【讨论】:

    • 如何从 $_SERVER['QUERY_STRING'] 执行此操作?
    • 有parse_url() 和parse_str(),但$_GET 始终可用且已填充,因此没有必要。
    • 我不会修改 $_GET 变量(或任何超全局变量),因为应用程序的其他地方可能需要它们。相反,创建一个临时变量,如$edit_query = $_GET;。然后你可以unset($edit_query['edit']); 而不影响你的超全局变量。
    【解决方案2】:

    这是我的镜头:

    /**
    *   Receives a URL string and a query string to remove. Returns URL without the query string
    */
    function remove_url_query($url, $key) {
        $url = preg_replace('/(?:&|(\?))' . $key . '=[^&]*(?(1)&|)?/i', "$1", $url);
        $url = rtrim($url, '?');
        $url = rtrim($url, '&');
        return $url;
    }
    

    返回:

    remove_url_query('http://example.com?a', 'a') => http://example.com
    remove_url_query('http://example.com?a=1', 'a') => http:/example.com
    remove_url_query('http://example.com?a=1&b=2', 'a') => http://example.com?b=2
    

    向大卫·沃尔什致敬。

    【讨论】:

      【解决方案3】:

      另一种解决方案,也可以避免&amp;amp; 问题!!!

      remove_query('http://example.com/?a=valueWith**&amp;**inside&b=value');
      

      代码:

      function remove_query($url, $which_argument=false){ 
          return preg_replace('/'. ($which_argument ? '(\&|)'.$which_argument.'(\=(.*?)((?=&(?!amp\;))|$)|(.*?)\b)' : '(\?.*)').'/i' , '', $url);  
      }
      

      【讨论】:

        【解决方案4】:

        如果edit=1 是第一个变量,它将不起作用:

        listitems.php?edit=1&color=green&...
        

        您可以使用$_GET 变量自己创建查询字符串。比如:

        $qs = '';
        foreach ($_GET as $key => $value){
            if ($key  == 'pagenum'){
                // Replace page number
                $qs .= $key  . '=' . $new_page_num . '&';
            }elseif ($key  != 'edit'){
                // Keep all key/values, except 'edit'
                $qs .= $key  . '=' . urlencode($value) . '&';
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2011-07-28
          • 1970-01-01
          • 2021-12-08
          • 1970-01-01
          • 2014-01-26
          • 2013-07-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多