問題描述
假設我請求此 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 中保留加號?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!