在 url 中传递一个百分比 (%) 符号并使用 php 获取它的确切值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17342671/
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
Pass a percent (%) sign in a url and get exact value of it using php
提问by Sumit Bijvani
I am trying to pass percent (%) sign in url like
我正在尝试通过百分比(%)登录网址,例如
%B6011000995504101^SB
but when I echo, it returns
但是当我回声时,它返回
?011000995504101^SB
I want exact same value as I pass it in URL.
我想要与在 URL 中传递的值完全相同的值。
I have tried to use urlencode() function, but it give me output like this...
我曾尝试使用 urlencode() 函数,但它给了我这样的输出......
%B6011000995504101%5ESB
please help me regarding this
请帮我解决这个问题
回答by IanPudney
Answer:
回答:
To send a %
sign in a url, instead send %25
.
要%
在 url 中发送签名,请改为发送%25
.
In your case, in order for php to see a percent sign, you must pass the character string %25B6011000995504101^SB
to the server.
在您的情况下,为了让 php 看到百分号,您必须将字符串传递%25B6011000995504101^SB
给服务器。
Why:
为什么:
In URLs, the percent sign has special meaning. Is used to encode special characters. For example, &
is the separator between parameters, so if you want your parameter to actually containan &
, you instead write %26
. Because the percent sign is used to encode special characters, it is alsoa special character, and so if you want to actually send a percent sign, it must also be encoded. The encoding for a percent sign is %25
.
在 URL 中,百分号具有特殊含义。用于编码特殊字符。例如,&
是参数之间的分隔符,因此如果您希望参数实际包含一个&
,则改为编写%26
. 因为百分号是用来对特殊字符进行编码的,它也是一种特殊字符,所以如果要实际发送一个百分号,也必须对其进行编码。百分号的编码是%25
.
回答by Nate from Kalamazoo
Before including a raw string in a URL it's a good idea to pass it through urlencode like so:
在 URL 中包含原始字符串之前,最好像这样通过 urlencode 传递它:
<?php
$original='%B6011000995504101^SB';
$updated=urlencode($original);
echo "<a href=\"some_page.php?$updated\">Link here</a>";
?>
The receiving page will know what to do - give this example a try on your webserver:
接收页面会知道该怎么做 - 在您的网络服务器上试试这个例子:
<?php
if($_GET['argument']) {
echo "<p>You passed in the argument "$_GET[argument]"</p>";
}else {
echo "<p>No argument was passed.</p>";
}
$original='%B6011000995504101^SB';
$updated=urlencode($original);
echo "<a href=\"urlencode.php?argument=$updated\">Link here</a>";
?>
(name the file urlencode.php)
(命名文件urlencode.php)