Update:

Thanks to Hakre and Martino for their answers: The function works alot better now:

$queryString = $_SERVER['QUERY_STRING']; 
parse_str($queryString, $params);

    foreach ($params as $key => $term)
    {
        $tags = explode(' ',$term);
        $tagCount = count($tags);

        //If there is more than one term per key, break them up
        if($tagCount > 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('=+', '++', '+&'), array('=', '+', '&'), $urlx);
                    if(substr($urlx, -1) == '+'){
                     $urlx = substr($urlx,0,-1);
                    }
                    echo '<a href="?'.$urlx.'">'.$tag.'</a><br/>';
                }
            }
        } else {
            //If there's just one term per key get rid of the key/term pair
            $these = array_diff_assoc($params, array($key => $term));
            printf("<a href=\"?%s\">%s</a><br>\n", http_build_query($these), $term);    
        }
    }

If anyone has any further suggestions on making this snippet better I'd really appreciate it!

Thanks


original question

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.

Does anyone have a better or more elegant way to write this?

Thanks

 <?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('+&','&',$urlx);
                echo '<a href="?'.$urlx.'">'.$tag.'</a><br/>';
            }
            else {
                $term = explode('=',$tag);
                $urlx = str_replace($term[1], '', $queryString);
                $urlx = str_replace('=+','=',$urlx);
                $urlx = str_replace('++','+',$urlx);
                $urlx = str_replace('+&','&',$urlx);
                echo '<a href="?'.$urlx.'">'.$term[1].'</a><br/>';
            }
        }
    }
    ?>

Sample output would be the following:

Query string: ?style=automotive&type=commercial+residential

HTML output:

<a href="?type=commercial+residential">automotive</a><br/>
<a href="?style=automotive&type=residential">commercial</a><br/>
<a href="?style=automotive&type=commercial">residential</a><br/>
share|improve this question
1  
Please add sample input and output of this function. It will help us understand what you are trying to accomplish. – Abbas Dec 22 '11 at 20:21

2 Answers

PHP has built in functions to solve your issue, one to parse a query string and one to compile one again: parse_str and http_build_query:

parse_str($queryString, $params);

foreach ($params as $key => $term)
{
    $these = array_diff_assoc($params, array($key => $term));
    printf("<a href=\"?%s\">%s</a><br>\n", http_build_query($these), $term);
}

Example output:

<a href="?b=b&c=c">a</a><br>
<a href="?a=a&c=c">b</a><br>
<a href="?a=a&b=b">c</a><br>

Demo

share|improve this answer
Thanks Hakre! That gets me most of the way. The only thing is there are often more than one term per key, for example: ?city=glendale+cypress&type=industrial+apartment, so your code groups together "<a href=""...>industrial apartment</a>" where I need them separated like "<a href="...">industrial</a>" and "<a href="...">apartment</a>" – j-man86 Dec 22 '11 at 21:13
Well, that was not visible to me in your question. I have the feeling that makes the thing much more complicated. I need to think about how to do that first. Maybe first creating these pairs as in the answer and then expanding each pair in case it has some more variation. – hakre Dec 22 '11 at 21:24

One thing you could do for sure is to make use of str_replace function accepting arrays:

$urlx = str_replace('=+','=',$urlx);
$urlx = str_replace('++','+',$urlx);
$urlx = str_replace('+&','&',$urlx);

becomes

$urlx = str_replace(array('=+', '++', '+&'), array('=', '+', '&'), $urlx);

Same for the else part

share|improve this answer

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.