Parse query string into an array

前端 未结 10 2391
暖寄归人
暖寄归人 2020-11-22 02:58

How can I turn a string below into an array?

pg_id=2&parent_id=2&document&video 

This is the

10条回答
  •  攒了一身酷
    2020-11-22 03:33

    Sometimes parse_str() alone is note accurate, it could display for example:

    $url = "somepage?id=123&lang=gr&size=300";
    

    parse_str() would return:

    Array ( 
        [somepage?id] => 123 
        [lang] => gr 
        [size] => 300 
    )
    

    It would be better to combine parse_str() with parse_url() like so:

    $url = "somepage?id=123&lang=gr&size=300";
    parse_str( parse_url( $url, PHP_URL_QUERY), $array );
    print_r( $array );
    

提交回复
热议问题