简体   繁体   中英

PHP Retrieve Keys From Query String, For each key: create URL removing that key from query string

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/>

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM