Let's say I am getting requests such as:
假设我收到如下请求:
http://www.example.com/index.php?id=123&version=3&id=234&version=4
http://www.example.com/index.php?id=123&version=3&id=234&version=4
Is it possible to extract these in a simple way inside my php code? I realize I could get the entire querystring with javascript using window.location.href and handle it manually but I'm looking for something more elegant. The requests can contain any number of version/id pairs but I can assume that the query is well-formed and have no obligation to handle invalid strings.
是否可以在php代码中以简单的方式提取它们?我意识到我可以使用windows .location使用javascript获得整个querystring。href并手动处理它,但我正在寻找更优雅的东西。请求可以包含任意数量的版本/id对,但是我可以假设查询是格式良好的,并且没有处理无效字符串的义务。
5 个解决方案
#1
7
According to this comment from the PHP manual, PHP's query string parser will drop duplicate params... so I don't think that PHP is a good fit for what you want to do (except in that it has the same capacity as javascript to get the raw query string, with which you can do whatever you want)
根据PHP手册中的这条注释,PHP的查询字符串解析器将删除重复的params……所以我认为PHP不适合您想要做的事情(除了它具有与javascript相同的获取原始查询字符串的能力,您可以使用它做任何您想做的事情)
#2
22
If you can change the field name to include []
, then PHP will create an array containing all of the matching values:
如果可以将字段名更改为include[],则PHP将创建一个包含所有匹配值的数组:
http://www.example.com/index.php?id[]=123&version[]=3&id[]=234&version[]=4
If you don't have the ability to change the field names, then as you say, you'll have to parse the querystring yourself.
如果您没有更改字段名的能力,那么正如您所说的,您必须自己解析querystring。
#3
1
Assuming you have some control over the request, suffix the name with []
and PHP will generate arrays instead of dropping all but one.
假设您对请求有一些控制,那么在名称后面加上[]和PHP将生成数组,而不是只删除一个。
http://www.example.com/index.php?id[]=123&version[]=3&id[]=234&version[]=4
Since they are pairs you'll probably want to fix the order they appear in using indexes.
因为它们是成对的,所以您可能想要修复它们在使用索引时出现的顺序。
http://www.example.com/index.php?id[0]=123&version[0]=3&id[1]=234&version[1]=4
#4
1
Just extract the keys and values of $_GET, use the function as:
只需提取$_GET的键值和值,使用以下函数:
print_array('$_GET...',$_GET);
... and the function code will be:
…函数代码为:
function print_array($title, $arr) {
echo '<table width="100%" style="padding:10;">';
echo '<tr><td width="30%" style="text-align:right; background-color:bisque;">key of </td><td style="background-color:bisque;">'.$title.'</td></tr>';
foreach($arr as $key => $value) {
echo '<tr>';
echo '<td style="text-align:right; color:grey;">';
echo $key;
echo '</td>';
echo '<td>';
echo $value;
echo '</td>';
echo '</tr>';
}
echo '</table>';
}
#5
0
Not as rounded or reliable as methods mentioned above but I use this to remove the need to []
in urls without worrying about rewriting.
不像上面提到的方法那样全面和可靠,但是我使用它来删除url中的[]需求,而不用担心重写。
$aQuery = explode("&", $_SERVER['QUERY_STRING']);
$aQueryOutput = array();
foreach ($aQuery as $param) {
if(!empty($param)){
$aTemp = explode('=', $param, 2);
if(isset($aTemp[1]) && $aTemp[1] !== ""){
list($name, $value) = explode('=', $param, 2);
$aQueryOutput[ strtolower(urldecode($name)) ][] = urldecode(preg_replace('/[^a-z 0-9\'+-]/i', "", $value));
}
}
}
#1
7
According to this comment from the PHP manual, PHP's query string parser will drop duplicate params... so I don't think that PHP is a good fit for what you want to do (except in that it has the same capacity as javascript to get the raw query string, with which you can do whatever you want)
根据PHP手册中的这条注释,PHP的查询字符串解析器将删除重复的params……所以我认为PHP不适合您想要做的事情(除了它具有与javascript相同的获取原始查询字符串的能力,您可以使用它做任何您想做的事情)
#2
22
If you can change the field name to include []
, then PHP will create an array containing all of the matching values:
如果可以将字段名更改为include[],则PHP将创建一个包含所有匹配值的数组:
http://www.example.com/index.php?id[]=123&version[]=3&id[]=234&version[]=4
If you don't have the ability to change the field names, then as you say, you'll have to parse the querystring yourself.
如果您没有更改字段名的能力,那么正如您所说的,您必须自己解析querystring。
#3
1
Assuming you have some control over the request, suffix the name with []
and PHP will generate arrays instead of dropping all but one.
假设您对请求有一些控制,那么在名称后面加上[]和PHP将生成数组,而不是只删除一个。
http://www.example.com/index.php?id[]=123&version[]=3&id[]=234&version[]=4
Since they are pairs you'll probably want to fix the order they appear in using indexes.
因为它们是成对的,所以您可能想要修复它们在使用索引时出现的顺序。
http://www.example.com/index.php?id[0]=123&version[0]=3&id[1]=234&version[1]=4
#4
1
Just extract the keys and values of $_GET, use the function as:
只需提取$_GET的键值和值,使用以下函数:
print_array('$_GET...',$_GET);
... and the function code will be:
…函数代码为:
function print_array($title, $arr) {
echo '<table width="100%" style="padding:10;">';
echo '<tr><td width="30%" style="text-align:right; background-color:bisque;">key of </td><td style="background-color:bisque;">'.$title.'</td></tr>';
foreach($arr as $key => $value) {
echo '<tr>';
echo '<td style="text-align:right; color:grey;">';
echo $key;
echo '</td>';
echo '<td>';
echo $value;
echo '</td>';
echo '</tr>';
}
echo '</table>';
}
#5
0
Not as rounded or reliable as methods mentioned above but I use this to remove the need to []
in urls without worrying about rewriting.
不像上面提到的方法那样全面和可靠,但是我使用它来删除url中的[]需求,而不用担心重写。
$aQuery = explode("&", $_SERVER['QUERY_STRING']);
$aQueryOutput = array();
foreach ($aQuery as $param) {
if(!empty($param)){
$aTemp = explode('=', $param, 2);
if(isset($aTemp[1]) && $aTemp[1] !== ""){
list($name, $value) = explode('=', $param, 2);
$aQueryOutput[ strtolower(urldecode($name)) ][] = urldecode(preg_replace('/[^a-z 0-9\'+-]/i', "", $value));
}
}
}