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

发布于 2024-12-22 08:16:48 字数 2936 浏览 1 评论 0原文

更新:

感谢 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,该 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

HTML 输出:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒几许 2024-12-29 08:16:48

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>

演示

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

贱人配狗天长地久 2024-12-29 08:16:48

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

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

变得相同

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

对于 else 部分

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文