1

I have this type of url:

http://mysite.com/results.php?location=montreal&car-rental=no&add-hotel=no&moods%5B%5D=Adventure&moods%5B%5D=Arts&moods%5B%5D=Culture&moods%5B%5D=Well-being&moods%5B%5D=Zen&weather-dependent=yes

In PHP, what's the best way to get only the $_GET[] part ?

Fo example:

$get = location=montreal&car-rental=no&add-hotel=no&moods%5B%5D=Adventure&moods%5B%5D=Arts&moods%5B%5D=Culture&moods%5B%5D=Well-being&moods%5B%5D=Zen&weather-dependent=yes

Thanks.

2

2 Answers 2

4

Are you searching something like:

$_SERVER['QUERY_STRING']
Sign up to request clarification or add additional context in comments.

Comments

2

I suppose you're looking for this:

$str = 'http://mysite.com/results.php?location=montreal&car-rental=no&add-hotel=no&moods%5B%5D=Adventure&moods%5B%5D=Arts&moods%5B%5D=Culture&moods%5B%5D=Well-being&moods%5B%5D=Zen&weather-dependent=yes';

$query = parse_url($str, PHP_URL_QUERY);
parse_str($query, $arr);
print_r($arr);

/*
Array
(
    [location] => montreal
    [car-rental] => no
    [add-hotel] => no
    [moods] => Array
        (
            [0] => Adventure
            [1] => Arts
            [2] => Culture
            [3] => Well-being
            [4] => Zen
        )

    [weather-dependent] => yes
)
*/

Codepad.

Explanation: parse_url($str, PHP_URL_QUERY) extracts the query string from that url, and parse_str($query, $arr), well, parses it, filling $arr variable with key => value pairs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.