PHP:将 url 参数解析为变量的最快方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8643938/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:09:11  来源:igfitidea点击:

PHP: Fastest method to parse url params into variables?

phpurlparameters

提问by mate64

Possible Duplicate:
Parse query string into an array

可能的重复:将
查询字符串解析为数组

What's the fastest method, to parse a string of url parameters into a array of accessible variables?

将一串 url 参数解析为一组可访问变量的最快方法是什么?

$current_param = 'name=Peter&car=Volvo&pizza=Diavola&....';

//results in a nice array that I can pass:

$result = array ( 
'name'  => 'Peter',
'car'   => 'Volvo',
'pizza' => 'Diavola'
)

I've tested a REGEXP, but this takes way too long. My script needs to parse about 10000+ url's at once sometimes :-(

我已经测试了 REGEXP,但这需要太长时间。我的脚本有时需要一次解析大约 10000 多个 url :-(

KISS - keep it simple, stupid

KISS - 保持简单,愚蠢

回答by kba

Use parse_str().

使用parse_str().

$current_param = "name=Peter&car=Volvo&pizza=Diavola";
parse_str($current_param, $result);
print_r($result);

The above will output

以上将输出

Array
(
    [name] => Peter
    [car] => Volvo
    [pizza] => Diavola
)

回答by Manigandan Arjunan

the parse_str() can do the trick as you expect

parse_str() 可以如你所愿

<?php
    $str = "first=value&arr[]=foo+bar&arr[]=baz";
    parse_str($str);
    echo $first;  // value
    echo $arr[0]; // foo bar
    echo $arr[1]; // baz

    parse_str($str, $output);
    echo $output['first'];  // value
    echo $output['arr'][0]; // foo bar
    echo $output['arr'][1]; // baz

    ?>