如何通过'&'在URL中的符号内值? [英] how do to pass '&' symbol inside value in URL?
问题描述
我将查询传递给我的搜索结果页面,变量为:?s =
。
I was passing the query to my search results page with variable: ?s=
.
测试网站时,我发现,当查询中有一个&
符号时,当我尝试检索 $ _ GET ['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.
我尝试使用&
,并将其转换为%26
我仍然有同样的问题。
I tried with &
and also converting it to %26
and I'm still having the same problem.
如何对其进行编码以将其传递到URL?
How can I encode them to pass them in URL?
编辑:
我指出我尝试将其编码为%26
,但仍然没有
I pointed that I tried encoding it to %26
and it still didn't work.
我尝试使用 urlencode()
和?s =比萨饼%26pasta
当我 print_r($ _ GET)
我得到:
I tried with urlencode()
and on ?s=pizza%26pasta
when I print_r($_GET)
I get:
Array ( [s] => pizza [pasta] => )
编辑2:
我刚刚发现问题实际上与 .htaccess
有关,似乎转换'& something ='中的'& something'。不知道如何解决它在htaccess。
I just found out that the problem actually has to do with .htaccess
it seems to transform the '&something' in '&something='. No idea how to fix it in htaccess.
这是我的.htaccess文件:
This is my .htaccess file:
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=$1 [L,QSA]
推荐答案
编码amersand(& )到%26
是正确的方法。
Url encoding the amersand (&) to %26
is the correct way to do this.
使用 http:/ /yoursite.com?var1=this%26that&var2=other
将导致您的 $ _ GET
具有两个变量的超全局数组
Using http://yoursite.com?var1=this%26that&var2=other
will result in your $_GET
superglobal array having two variables
$_GET['var1'] = 'this&that';
$_GET['var2'] = 'other';
您可以使用 urlencode
自动编码需要编码的所有字符。这些通常是用于构成网址组成部分的字符。即符号(@),冒号(:),问号(?)等。
You can use the function urlencode
to 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.
var_dump(urlencode('one&two'));
string(9) "one%26two"
这篇关于如何通过'&'在URL中的符号内值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!