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

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

问题描述

好的,我已经使用 mod_rewrite 和 PHP 编写了一个 REST API 实现.我正在通过 HTTP DELETE 请求的正文接受一个查询字符串(...集体呻吟?).撇开前面两个语句是否明智的争论不谈,我发现 PHP 不会自动解析 DELETE 请求的请求正文(即,尽管表单编码的查询字符串出现在请求正文中,但 $_POST 为空).这并没有让我特别惊讶.我确实发现令人惊讶的是,我一直无法找到用于解析查询字符串的内置 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 &urldecode 来清理表单编码的值...这是一种不同的编码,但我也无法辨别应该使用哪个 PHP 函数来解码表单编码的数据.

... 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.

任何建议将不胜感激.

推荐答案

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天全站免登陆