Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest method to parse URL parameters into variables in PHP [duplicate]

Possible Duplicate:
Parse query string into an array

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

$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 regular expression, but this takes way too long. My script needs to parse about 10000+ URLs at once sometimes :-(

KISS - keep it simple, stupid

like image 828
mate64 Avatar asked Dec 27 '11 11:12

mate64


People also ask

Which method will pass the value via URL?

Submitting form values through GET method A web form when the method is set to GET method, it submits the values through URL.

What is Parse_str in PHP?

Definition and Usage. The parse_str() function parses a query string into variables. Note: If the array parameter is not set, variables set by this function will overwrite existing variables of the same name.


1 Answers

Use 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
)
like image 69
kba Avatar answered Oct 01 '22 00:10

kba