PHP 从查询字符串中检索键,对于每个键:创建 URL 从查询字符串中删除该键
更新:
感谢 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
PHP 具有内置函数来解决您的问题,一个用于解析查询字符串,一个用于再次编译查询字符串:
parse_str
和
http_build_query
:示例输出:
演示
PHP has built in functions to solve your issue, one to parse a query string and one to compile one again:
parse_str
andhttp_build_query
:Example output:
Demo
您可以肯定做的一件事是使用接受数组的
str_replace
函数:变得相同
对于
else
部分One thing you could do for sure is to make use of
str_replace
function accepting arrays:becomes
Same for the
else
part