解析 URL 查询字符串

问题描述 投票:0回答:5

我有一个像这样的字符串:

$q = "menu=true&submenu=true&pcode=123456&code=123456";

我想获取

pcode = 123456
code = 123456
的值。

我怎样才能得到这个?

php string query-string text-parsing
5个回答
4
投票

如果不是来自 url,则使用

parse_str
函数(然后使用
$_GET
数组)

http://ru2.php.net/manual/en/function.parse-str.php


3
投票

使用explode从字符串中获取数组。

explode('&',$q);

它将在每个 & 字符上分解字符串并返回数组中的片段。


1
投票

参见parse_str


0
投票
    $q ="menu=true&submenu=true&pcode=123456&code=123456" ;

    // parse str values into an array called $pairs
    parse_str($q, $pairs);

   // loop through $pairs and display all values
    foreach ($pairs as $key => $val) {
       echo "$key => $val\n";
    }

0
投票

请使用php的explode函数来完成

例如:

   <?php

$q ="menu=true&submenu=true&pcode=123456&code=123456" ;
$pieces = explode("&",$q);

print_r($pieces);

foreach($pieces as $key=>$value){
    $newVarible[]=explode("=",$value);


}
print_r($newVarible);

?>
© www.soinside.com 2019 - 2024. All rights reserved.