[英]Parse query string into an array
How can I turn a string below into an array ?如何将下面的字符串转换为数组?
pg_id=2&parent_id=2&document&video
This is the array I am looking for,这是我正在寻找的数组,
array(
'pg_id' => 2,
'parent_id' => 2,
'document' => ,
'video' =>
)
Sometimes parse_str()
alone is note accurate, it could display for example:有时单独的
parse_str()
是注释准确的,它可以显示例如:
$url = "somepage?id=123&lang=gr&size=300";
parse_str() would return: parse_str() 将返回:
Array (
[somepage?id] => 123
[lang] => gr
[size] => 300
)
It would be better to combine parse_str()
with parse_url()
like so:将
parse_str()
与parse_url()
结合起来会更好,如下所示:
$url = "somepage?id=123&lang=gr&size=300";
parse_str( parse_url( $url, PHP_URL_QUERY), $array );
print_r( $array );
Using parse_str()
.使用
parse_str()
。
$str = 'pg_id=2&parent_id=2&document&video';
parse_str($str, $arr);
print_r($arr);
If you're having a problem converting a query string to an array because of encoded ampersands如果由于编码的&符号而将查询字符串转换为数组时遇到问题
&
then be sure to use html_entity_decode
那么一定要使用
html_entity_decode
Example:例子:
// Input string //
$input = 'pg_id=2&parent_id=2&document&video';
// Parse //
parse_str(html_entity_decode($input), $out);
// Output of $out //
array(
'pg_id' => 2,
'parent_id' => 2,
'document' => ,
'video' =>
)
Use http://us1.php.net/parse_str使用http://us1.php.net/parse_str
Attention, its usage is:注意,它的用法是:
parse_str($str, &$array);
not不是
$array = parse_str($str);
Please note that the above only applies to PHP version 5.3 and earlier .请注意,以上仅适用于 PHP 5.3 及更早版本。 Call-time pass-by-reference has been removed in PHP 5.4.
PHP 5.4 中删除了调用时传递引用。
There are several possible methods, but for you, there is already a built-in parse_str
function :有几种可能的方法,但对你来说,已经有一个内置的
parse_str
函数:
$array = array();
parse_str($string, $array);
var_dump($array);
这是将查询从当前 URL 解析为数组的单行代码:
parse_str($_SERVER['QUERY_STRING'], $query);
You can use the PHP string function parse_str()
followed by foreach
loop.您可以使用 PHP 字符串函数
parse_str()
后跟foreach
循环。
$str="pg_id=2&parent_id=2&document&video";
parse_str($str,$my_arr);
foreach($my_arr as $key=>$value){
echo "$key => $value<br>";
}
print_r($my_arr);
But PHP already comes with a built in $_GET function. this will convert it to the array by itself.但是 PHP 已经带有一个内置的 $_GET function。这将把它自己转换成数组。
try print_r($_GET)
and you will get the same results.试试
print_r($_GET)
,你会得到相同的结果。
You can try this code:你可以试试这个代码:
<?php
$str = "pg_id=2&parent_id=2&document&video";
$array = array();
parse_str($str, $array);
print_r($array);
?>
Output:输出:
Array
(
[pg_id] => 2
[parent_id] => 2
[document] =>
[video] =>
)
This is the PHP code to split a query in MySQL and SQL Server :这是在MySQL和SQL Server 中拆分查询的 PHP 代码:
function splitquery($strquery)
{
$arrquery = explode('select', $strquery);
$stry = ''; $strx = '';
for($i=0; $i<count($arrquery); $i++)
{
if($i == 1)
{
echo 'select ' . trim($arrquery[$i]);
}
elseif($i > 1)
{
$strx = trim($arrquery[($i-1)]);
if(trim(substr($strx,-1)) != '(')
{
$stry = $stry . '
select ' . trim($arrquery[$i]);
}
else
{
$stry = $stry.trim('select ' . trim($arrquery[$i]));
}
$strx = '';
}
}
return $stry;
}
Example:例子:
Select xx from xx select xx,(select xx) from xx where y=' cc'
select xx from xx left join (select xx) where (select top 1 xxx from xxx) oder by xxx desc";
select xx from xx
select xx,(select xx) from xx where y=' cc'
select xx from xx left join (select xx) where (select top 1 xxx from xxx) oder by xxx desc
For this specific question the chosen answer is correct but if there is a redundant parameter—like an extra "e"—in the URL the function will silently fail without an error or exception being thrown:对于这个特定问题,选择的答案是正确的,但如果 URL 中存在冗余参数(如额外的“e”),则该函数将静默失败,而不会引发错误或异常:
a=2&b=2&c=5&d=4&e=1&e=2&e=3
So I prefer using my own parser like so:所以我更喜欢像这样使用我自己的解析器:
//$_SERVER['QUERY_STRING'] = `a=2&b=2&c=5&d=4&e=100&e=200&e=300`
$url_qry_str = explode('&', $_SERVER['QUERY_STRING']);
//arrays that will hold the values from the url
$a_arr = $b_arr = $c_arr = $d_arr = $e_arr = array();
foreach( $url_qry_str as $param )
{
$var = explode('=', $param, 2);
if($var[0]=="a") $a_arr[]=$var[1];
if($var[0]=="b") $b_arr[]=$var[1];
if($var[0]=="c") $c_arr[]=$var[1];
if($var[0]=="d") $d_arr[]=$var[1];
if($var[0]=="e") $e_arr[]=$var[1];
}
var_dump($e_arr);
// will return :
//array(3) { [0]=> string(1) "100" [1]=> string(1) "200" [2]=> string(1) "300" }
Now you have all the occurrences of each parameter in its own array, you can always merge them into one array if you want to.现在您在自己的数组中拥有每个参数的所有出现次数,如果您愿意,您可以随时将它们合并到一个数组中。
Hope that helps!希望有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.