title: PHP Retrieve Keys From Query String, For each key: create URL removing that key from query string
body: <p><em><strong>Update:</em></strong></p>
  
  <p>Thanks to Hakre and Martino for their answers: The function works alot better now:</p>
  
  <pre><code>$queryString = $_SERVER['QUERY_STRING']; 
  parse_str($queryString, $params);
  
      foreach ($params as $key =&gt; $term)
      {
          $tags = explode(' ',$term);
          $tagCount = count($tags);
  
          //If there is more than one term per key, break them up
          if($tagCount &gt; 1) {
              foreach($tags as $tag) {
                  if($term != '') {
                      //remove individual term from query string and remove any redundant characters
                      $urlx = str_replace($tag, '', $queryString);
                      $urlx = str_replace(array('=+', '++', '+&amp;'), array('=', '+', '&amp;'), $urlx);
                      if(substr($urlx, -1) == '+'){
                       $urlx = substr($urlx,0,-1);
                      }
                      echo '&lt;a href="?'.$urlx.'"&gt;'.$tag.'&lt;/a&gt;&lt;br/&gt;';
                  }
              }
          } else {
              //If there's just one term per key get rid of the key/term pair
              $these = array_diff_assoc($params, array($key =&gt; $term));
              printf("&lt;a href=\"?%s\"&gt;%s&lt;/a&gt;&lt;br&gt;\n", http_build_query($these), $term);    
          }
      }
  </code></pre>
  
  <p>If anyone has any further suggestions on making this snippet better I'd really appreciate it! </p>
  
  <p>Thanks</p>
  
  <hr>
  
  <p><strong>original question</strong></p>
  
  <p>I'm trying to create a function that allows the user to "X out" or clear out of a given search filter. I've written a function (in a very hacked together manner) that gets the keys from each GET variable and then creates a url which would remove that key from the search string.</p>
  
  <p>Does anyone have a better or more elegant way to write this?</p>
  
  <p>Thanks</p>
  
  <pre><code> &lt;?php
      $queryString = $_SERVER['QUERY_STRING']; 
      $getArray = explode("=", $queryString); 
  
      foreach($getArray as $get) {
          $tagArray = explode("+",$get);
          foreach($tagArray as $tag){
              $pos = strpos($tag,'=');
              if($pos === false) {
                  $urlx = str_replace($tag, '', $queryString);
                  $urlx = str_replace('=+','=',$urlx);
                  $urlx = str_replace('++','+',$urlx);
                  $urlx = str_replace('+&amp;','&amp;',$urlx);
                  echo '&lt;a href="?'.$urlx.'"&gt;'.$tag.'&lt;/a&gt;&lt;br/&gt;';
              }
              else {
                  $term = explode('=',$tag);
                  $urlx = str_replace($term[1], '', $queryString);
                  $urlx = str_replace('=+','=',$urlx);
                  $urlx = str_replace('++','+',$urlx);
                  $urlx = str_replace('+&amp;','&amp;',$urlx);
                  echo '&lt;a href="?'.$urlx.'"&gt;'.$term[1].'&lt;/a&gt;&lt;br/&gt;';
              }
          }
      }
      ?&gt;
  </code></pre>
  
  <p>Sample output would be the following:</p>
  
  <p>Query string: 
  <code>?style=automotive&amp;type=commercial+residential</code></p>
  
  <p>HTML output: </p>
  
  <pre><code>&lt;a href="?type=commercial+residential"&gt;automotive&lt;/a&gt;&lt;br/&gt;
  &lt;a href="?style=automotive&amp;type=residential"&gt;commercial&lt;/a&gt;&lt;br/&gt;
  &lt;a href="?style=automotive&amp;type=commercial"&gt;residential&lt;/a&gt;&lt;br/&gt;
  </code></pre>