php 如何在 URL 值中传递“&”符号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11771995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how do to pass '&' symbol inside value in URL?
提问by lisovaccaro
I was passing the query to my search results page with variable: ?s=.
我正在使用变量将查询传递到我的搜索结果页面:?s=。
I was testing the site and I discovered than when the query has a &symbol in it, when I try to retrieve $_GET['s']it gets cut in the & symbol.
我正在测试该站点,我发现当查询中有一个&符号时,当我尝试检索$_GET['s']它时,它会在 & 符号中被剪切。
I tried with &and also converting it to %26and I'm still having the same problem.
我尝试&并将其转换为%26,但仍然遇到同样的问题。
How can I encode them to pass them in URL?
如何编码它们以在 URL 中传递它们?
EDIT:
编辑:
I pointed that I tried encoding it to %26and it still didn't work.
我指出我尝试将其编码为%26,但仍然无法正常工作。
I tried with urlencode()and on ?s=pizza%26pastawhen I print_r($_GET)I get:
我试着urlencode()和?s=pizza%26pasta我print_r($_GET),我得到:
Array ( [s] => pizza [pasta] => )
EDIT 2:
编辑2:
I just found out that the problem actually has to do with .htaccessit seems to transform the '&something' in '&something='. No idea how to fix it in htaccess.
我刚刚发现问题实际上与.htaccess它似乎转换了“&something=”中的“&something”有关。不知道如何在 htaccess 中修复它。
This is my .htaccess file:
这是我的 .htaccess 文件:
RewriteEngine On
RewriteBase /
# remove www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]
# rewrite to action
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]+)/?$ /a/?s= [L,QSA]
回答by Leigh
Url encoding the amersand (&) to %26is the correct way to do this.
将 amerand (&) 编码为的 URL%26是执行此操作的正确方法。
Using http://yoursite.com?var1=this%26that&var2=otherwill result in your $_GETsuperglobal array having two variables
使用http://yoursite.com?var1=this%26that&var2=other将导致您的$_GET超全局数组具有两个变量
$_GET['var1'] = 'this&that';
$_GET['var2'] = 'other';
You can use the function urlencodeto automatically encode all characters that require encoding. These are typically the characters that are used to make up the component parts of a url. i.e. the At symbol (@), colon (:), question mark (?), etc.
您可以使用该功能urlencode对所有需要编码的字符进行自动编码。这些通常是用于构成 url 组成部分的字符。即 At 符号 (@)、冒号 (:)、问号 (?) 等。
var_dump(urlencode('one&two'));
string(9) "one%26two"
回答by itsols
Use these steps:
使用以下步骤:
- First build your string in PHP
- Then use the function urlencode()to correctly encode your string.
- When using $_GET[]make sure you use urldecode()to get the variable to its original format.
- 首先在 PHP 中构建你的字符串
- 然后使用函数urlencode()正确编码您的字符串。
- 使用$_GET[] 时,请确保使用urldecode()将变量恢复为原始格式。
Hope this helps :)
希望这可以帮助 :)
回答by lisovaccaro
This is a raw patch I built that takes the array and looks for blank keys and merges them on a string. It only works when a single variable is passed though.
这是我构建的原始补丁,它接受数组并查找空白键并将它们合并到一个字符串上。它仅在传递单个变量时才有效。
function arrayToString($array) {
$string = '';
foreach ($array as $key => $value) {
if($value) {
$string = $string.'&'.$value;
} else {
$string = $string.'&'. $key;
}
}
return substr($string,1,strlen($string));
}
This is for more than one variable in case someone finds it useful:
这适用于多个变量,以防有人发现它有用:
function arrayToString($array) {
$string = '';
foreach ($array as $key => $value) {
if($value) {
$string = $string.'&'.$key.'='.$value;
} else {
$string = $string.'&'. $key;
}
}
return substr($string,1,strlen($string));
}
return $string
回答by Vinod Joshi
Suppose you want send following string into your url:
假设您想将以下字符串发送到您的网址中:
var1 = "1st Class Ink & Toner"
var2 = "Test1"
var3 = "test3"
Now using php,
现在使用php,
var1 = urlencode(var1); left remain the same.
then put them into url: http://url.com/x=var1&y=var2&z=var3
然后将它们放入网址:http: //url.com/x=var1&y=var2&z=var3
vKj
vKj

