是否可以在没有编码的情况下在 PHP $_GET vars 中保留加号? [英] Is it possible to preserve plus signs in PHP $_GET vars without encoding?

查看:22
本文介绍了是否可以在没有编码的情况下在 PHP $_GET vars 中保留加号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我请求此 URL:

Say I request this URL:

http://mydomain.com/script.php?var=2+2

$_GET['var'] 现在将是:2 2",它应该是2+2"

$_GET['var'] will now be: "2 2" where it should be "2+2"

显然我可以在发送之前对数据进行编码然后解码,但我想知道这是否是唯一的解决方案.我也可以用加号替换空格,但我也想允许空格.我只想要传递的任何字符,而无需进行任何 url 解码或编码.谢谢!

Obviously I could encode the data before sending and then decode it, but I'm wondering if this is the only solution. I could also replace spaces with plus symbols, but I want to allow spaces as well. I simply want whatever characters were passed, without any url decoding or encoding going on. Thank you!

推荐答案

当然.你可以读出 $_SERVER["QUERY_STRING"],自己分解,然后放弃通常的 URL 解码,或者只将 %xx 占位符转换回来.

Of course. You could read out $_SERVER["QUERY_STRING"], break it up yourself, and then forgo the usual URL decoding, or only convert %xx placeholders back.

preg_match_all('/(w+)=([^&]+)/', $_SERVER["QUERY_STRING"], $pairs);
$_GET = array_combine($pairs[1], $pairs[2]);

(示例仅适用于字母数字参数,不进行上述 %xx 解码.只需分解原始输入.)

(Example only works for alphanumeric parameters, and doesn't do the mentioned %xx decoding. Just breaks up the raw input.)

这篇关于是否可以在没有编码的情况下在 PHP $_GET vars 中保留加号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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