2

This is $postfields value:

image=%40E%3A%5Cdev%5Cphoto.jpg&oauth_timestamp=1291739697&oauth_token=123456&tile=true

I need to take out any the image=xxxxx part of that value so i only have this at the end:

oauth_timestamp=1291739697&oauth_token=123456&tile=true

I tried explode() and parse_str() approaches with no success. The best solution would be to find any image=xxxxx inside the long string, then convert it to an array like this:

$array['image'] = '%40E%3A%5Cdev%5Cphoto.jpg';
$array['oauth_timestamp'] = '1291739697';
$array['oauth_token'] = '123456';
$array['tile'] = 'true';

That way is really easy to just unset($array['image']) and then implode() everything back. Any ideas on how to do that? Thanks!

asked Dec 7, 2010 at 16:39

3 Answers 3

3

Use parse_str() to take it apart, and http_build_query() to put it back together again.

Szymon
43k16 gold badges99 silver badges115 bronze badges
answered Dec 7, 2010 at 16:42

6 Comments

i tried that approach also. the problem is that sometimes there are more or less variables in the querystring... and because of that http_build_query becomes really cumbersome.
... How so? Take the array returned, remove the key, pass it back in.
but parse_str doesn't return any arrays. It only creates new variables based on the key and value of the long string php.net/parse_str
it would be great if parse_str would return the variable names as key names in an array.
... I don't think we were reading the same documentation then... "arr If the second parameter arr is present, variables are stored in this variable as array elements instead. "
|
1

use strpos of &oauth and then use substring

$rest = substr($post_data, strpos($post_data, '&oauth'));
answered Dec 7, 2010 at 16:42

4 Comments

@thedom, hey, thanks! Sometimes simpler is better. Though I believe @Ignacio's solution is better-served and more robust.
this works only if image goes first and before &oauth... what if image goes in the middle of the querystring between yet unknown variables? (i cannot control that :s)
I'm aware of the limitations, which is why I pointed out the other solution is inevitably a better-fit, but was just posting a lightweight solution. That's the joy of SO, you get many ways to skin a cat. ;-)
hehe, i'll stay with the parse_str() approach. Thanks for your support!
0

This should do the trick:

function removeVarFromQueryString($varToRemove, $originalQs=null) {
 if (!$originalQs) {
 $originalQs = $_SERVER['QUERY_STRING'];
 }
 $params = [];
 parse_str($originalQs, $params);
 unset($params[$varToRemove]);
 return http_build_query($params);
}

Then for your example:

$qs = "image=%40E%3A%5Cdev%5Cphoto.jpg&oauth_timestamp=1291739697&oauth_token=123456&tile=true";
echo removeVarFromQueryString("image", $qs); // => oauth_timestamp=1291739697&oauth_token=123456&tile=true
answered Mar 1, 2017 at 17:26

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.