是否可以在没有编码的情况下在 PHP $_GET vars 中保留加号?

2021-12-27 00:00:00 encoding get php

假设我请求此 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.)

相关文章