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!
3 Answers 3
Use parse_str()
to take it apart, and http_build_query()
to put it back together again.
6 Comments
use strpos of &oauth and then use substring
$rest = substr($post_data, strpos($post_data, '&oauth'));
4 Comments
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