php 将查询字符串解析为数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5397726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parse query string into an array
提问by laukok
How can I turn a stringbelow 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' =>
)
回答by Anthony
回答by yassine2020
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 );
回答by Rocket Hazmat
Using parse_str()
.
使用parse_str()
.
$str = 'pg_id=2&parent_id=2&document&video';
parse_str($str, $arr);
print_r($arr);
回答by Ionut Bajescu
Use http://us1.php.net/parse_str
使用http://us1.php.net/parse_str
Attention, it's usage is:
注意,它的用法是:
parse_str($str, &$array);
not
不是
$array = parse_str($str);
回答by Casper Wilkes
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' =>
)
回答by KingCrunch
There are several possible methods, but for you, there is already a builtin parse_str
function
有几种可能的方法,但对你来说,已经有一个内置parse_str
函数
$array = array();
parse_str($string, $array);
var_dump($array);
回答by hovado
This is one-liner for parsing query from current URL into array:
这是将查询从当前 URL 解析为数组的单行代码:
parse_str($_SERVER['QUERY_STRING'], $query);
回答by hovado
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);
回答by Max Muster
// ok, another method using c4 ..
// we have $input as string
$input = "pg_id=2&parent_id=2&document&video";
// looking for $output as array
$output= array();
// We knead a bit c4, and stick it to $input and turn it to pieces ...
$pieces = explode("&",$input);
// we pick up all the pieces and make the output array
foreach($pieces as $k => $v){
// the new key is what is before the "="
$var[$k] = explode("=",$pieces[$k])[0];
// and the new value is what is after the "="
$output[$var[$k]] = explode("=",$pieces[$k])[1];
}
// print_r($output);
Array
(
[pg_id] => 2
[parent_id] => 2
[document] =>
[video] =>
)
回答by Dewa Putra
This is the PHP code to split query in mysql & mssql
这是在 mysql 和 mssql 中拆分查询的 PHP 代码
enter code here
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:
例子:
Query before
查询之前
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 ";
Query after
查询后
select xx from xx
从 xx 中选择 xx
select xx,(select xx) from xx where y=' cc'
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 left join (select xx) where (select top 1 xxx from xxx) oder by xxx desc
Thank you, from Indonesia Sentrapedagang.com
谢谢,来自印度尼西亚Sentrapedagang.com