問題描述
假設(shè)我請(qǐng)求此 URL:
Say I request this URL:
http://mydomain.com/script.php?var=2+2
$_GET['var'] 現(xiàn)在將是:2 2",它應(yīng)該是2+2"
$_GET['var'] will now be: "2 2" where it should be "2+2"
顯然我可以在發(fā)送之前對(duì)數(shù)據(jù)進(jìn)行編碼然后解碼,但我想知道這是否是唯一的解決方案.我也可以用加號(hào)替換空格,但我也想允許空格.我只想要傳遞的任何字符,而無需進(jìn)行任何 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!
推薦答案
當(dāng)然.你可以讀出 $_SERVER["QUERY_STRING"]
,自己分解,然后放棄通常的 URL 解碼,或者只將 %xx
占位符轉(zhuǎn)換回來.
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]);
(示例僅適用于字母數(shù)字參數(shù),不進(jìn)行上述 %xx 解碼.只需分解原始輸入.)
(Example only works for alphanumeric parameters, and doesn't do the mentioned %xx decoding. Just breaks up the raw input.)
這篇關(guān)于是否可以在沒有編碼的情況下在 PHP $_GET vars 中保留加號(hào)?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!