在PHP解码查询字符串 [英] Decoding query strings in PHP

查看:127
本文介绍了在PHP解码查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,所以我写了使用mod_rewrite和PHP中的REST API的实现。我接受查询字符串通过HTTP的身体DELETE请求(...的集体呻吟?)。大约两个previous陈述一旁的智慧争论,有什么我发现的是,PHP不自动解析的删除请求(的请求体即$ _ POST尽管形式恩codeD查询出现在字符串为空请求体)。这并没有特别让我感到吃惊。我确实发现令人意外的是,我一直无法找到解析查询字符串内置PHP函数?难道我只是忽略了什么?我可以这样做:

Okay, so I've written a REST API implementation using mod_rewrite and PHP. I'm accepting a query string via the body of HTTP DELETE requests (... collective groan?). Arguments about the wisdom of both previous statements aside, what I've found is that PHP doesn't automatically parse the request body of DELETE requests (i.e. $_POST is empty despite form-encoded query string appearing in body of request). This didn't particularly surprise me. What I did find surprising was that I've been unable to find a built-in PHP function for parsing a query string?? Have I simply overlooked something? I can do something like:

public function parseQS($queryString, &$postArray){
  $queryArray = explode('&', $queryString);
  for($i = 0; $i < count($queryArray); $i++) {
    $thisElement = split('=', $queryArray[$i]);
    $postArray[$thisElement[0]] = htmlspecialchars(urldecode($thisElement[1]));
  }
}

...它只是似乎很奇怪,有不会是一个PHP内置的处理这个问题。此外,我怀疑我不应该使用htmlspecialcharacters和放大器上; urlde code擦洗形式恩codeD值......这是一种不同的编码方式,但我也有难以分辨,我应该使用脱code形式恩$的PHP函数C $ CD数据。

... it just seems odd that there wouldn't be a PHP built-in to handle this. Also, I suspect I shouldn't be using htmlspecialcharacters & urldecode to scrub form-encoded values... it's a different kind of encoding, but I'm also having trouble discerning which PHP function I should be using to decode form-encoded data.

任何建议将AP preciated。

Any suggestions will be appreciated.

推荐答案

有的 parse_str 。糟糕的名字,但你想要做什么。并注意它返回任何内容,第二个参数是按引用传递。

There's parse_str. Bad name, but does what you want. And notice that it returns nothing, the second argument is passed by reference.

这篇关于在PHP解码查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆