PHP 从查询字符串中检索键,对于每个键:创建从查询字符串中删除该键的 URL


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

更新:

感谢Hakre和Martino的回答: 该函数现在工作得更好了:

$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);    
        }
    }

如果有人对使这个片段更好有任何进一步的建议,我将不胜感激!

谢谢


原始问题

我正在尝试创建一个允许用户"X out"或清除给定搜索过滤器的函数。我编写了一个函数(以一种非常黑客的方式(,它从每个GET变量中获取键,然后创建一个url,该网址将从搜索字符串中删除该键。

没有人有更好或更优雅的方式来写这个?

谢谢

 <?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/>';
            }
        }
    }
    ?>

示例输出如下:

查询字符串: ?style=automotive&type=commercial+residential

网页输出:

<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 内置了函数来解决您的问题,一个用于解析查询字符串,另一个用于再次编译: parse_strhttp_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);
}

示例输出:

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

演示

你可以肯定做的一件事是利用str_replace函数接受数组:

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

成为

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

else部分相同