开发者

PHP: parsed_str help please

开发者 https://www.devze.com 2023-03-25 20:22 出处:网络
This snippet works fine: $url=\"Http://www.youtube.com/watch?v=upenR6n7xWY&feature=BFa&list=PL88ACC6CB00DC2B44&index=4\";

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() is开发者_StackOverflown't working as it should. Any ideas what I might be doing wrong?


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.


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.


Basically, parse_str converts

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

To

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

精彩评论

暂无评论...
验证码 换一张
取 消