PHP从字符串中删除querystring变量

时间:2010-12-07 16:39:44

标签: php string

这是 $ postfields 值:

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

我需要取出该值的 image = xxxxx 部分,所以我最后只有这个:

oauth_timestamp=1291739697&oauth_token=123456&tile=true

我尝试使用explode()和parse_str()方法但没有成功。最好的解决方案是在长字符串中找到任何 image = xxxxx ,然后将其转换为如下数组:

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

这种方式很容易被取消($ array ['image'])然后implode()返回所有内容。有关如何做到这一点的任何想法?谢谢!

3 个答案:

答案 0 :(得分:3)

使用parse_str()将其拆分,然后http_build_query()将其重新组合在一起。

答案 1 :(得分:1)

使用& oauth的strpos,然后使用substring

$rest = substr($post_data, strpos($post_data, '&oauth'));

答案 2 :(得分:0)

这应该可以解决问题:

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

    $params = [];
    parse_str($originalQs, $params);

    unset($params[$varToRemove]);
    return http_build_query($params);
}

然后为你的例子:

$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
相关问题