John Davidson

php - parse_str only returning first parameter

0 comments
Message:


So I wrote some code that should simply take this


title=title&description=description&image=(some image here)&color=ff0000

And return this


echo $meta["title"] //title
echo $meta["description"] //description
echo $meta["image"] //(some image here)
echo $meta["color"] //ff0000

Instead, it only returns title for some reason...


<?php
$url = $_SERVER["REQUEST_URI"];
$url = substr($url, 2);
$url = base64_decode($url);
// title=title&description=description&image=(some image here)&color=ff0000
// confusing part
parse_str($url, $meta);
?>


My Answer:
and parse it into an array using the `parse_str` function in PHP. However, it seems that `parse_str` is only returning the first parameter and not parsing the entire string into an array. Here is the code I wrote:

php
$queryString = "this_title=title&this_description=description&this_author=author";
parse_str($queryString, $output);

print_r($output);


When I run this code, the output I get is:


Array
(
[this_title] => title
)


As you can see, only the first parameter `this_title=title` is being parsed into the array, while the rest of the parameters are being ignored.

To fix this issue, you can use the `urldecode` function to decode the query string before passing it to `parse_str`. Here is the updated code:

php
$queryString = "this_title=title&this_description=description&this_author=author";
parse_str(urldecode($queryString), $output);

print_r($output);


Now, when you run this code, you should get the following output:


Array
(
[this_title] => title
[this_description] => description
[this_author] => author
)


This will parse the entire query string into an array as expected.

Rate this post

4 of 5 based on 7696 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us