Ryan
Ryan

Reputation: 10049

PHP: parsed_str help please

This snippet works fine:

$url="Http://www.youtube.com/watch?v=upenR6n7xWY&feature=BFa&list=PL88ACC6CB00DC2B44&index=4";

$parsed_url=parse_url($url);

echo "<br><br><pre>";
print_r(parse_url($url));
echo "</pre>";
echo $parsed_url['query'];

But when I add the below:

echo "<br><br><pre>";
$parsed_str=parse_str($parsed_url['query']);
print_r($parsed_str);
echo "</pre>";

Nothing happens. I suspect parse_str() isn't working as it should. Any ideas what I might be doing wrong?

Upvotes: 0

Views: 81

Answers (3)

Andrew Moore
Andrew Moore

Reputation: 95424

If you want to have the result of parse_str() in an array, pass the array as the second argument:

parse_str($parsed_url['query'], $parsed_str);

var_dump($parsed_str);

I'm going to assume that the user inputs the URL. If so, do not use parse_str without a second argument!. Doing so would result in a security risk where the user can overwrite arbitrary variables with the value of their choice.

Upvotes: 2

ace
ace

Reputation: 7593

Basically, parse_str converts

v=upenR6n7xWY&feature=BFa&list=PL88ACC6CB00DC2B44&index=4

To

$v = "upenR6n7xWY"
$feature = "BFa"
$list = "PL88ACC6CB00DC2B44"
$index = 4

Upvotes: 1

Ivan Peevski
Ivan Peevski

Reputation: 963

parse_str() doesn't return anything. It populates variables.

For example, if you have a query string of $query = "param=1&test=2"

after parse_str($query);

you can check var_dump($param) and var_dump($test) - those two variables would be created for you.

Upvotes: 1

Related Questions