如何在 PHP 中正确地对字符串进行 URL 编码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4744888/
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 to properly URL encode a string in PHP?
提问by Click Upvote
I'm making a search page, where you type a search query and the form is submitted to search.php?query=your query
. What PHP function is the best and that I should use for encoding/decoding the search query?
我正在制作一个搜索页面,您可以在其中键入搜索查询并将表单提交到search.php?query=your query
. 什么 PHP 函数是最好的,我应该使用它来编码/解码搜索查询?
回答by Gumbo
For the URI query use urlencode
/urldecode
; for anything else use rawurlencode
/rawurldecode
.
对于 URI 查询使用urlencode
/ urldecode
; 对于其他任何事情,请使用rawurlencode
/ rawurldecode
。
The difference between urlencode
and rawurlencode
is that
urlencode
和之间的区别rawurlencode
在于
urlencode
encodes according to application/x-www-form-urlencoded(space is encoded with+
) whilerawurlencode
encodes according to the plain Percent-Encoding(space is encoded with%20
).
urlencode
根据application/x-www-form-urlencoded 编码(空格用 编码+
),而rawurlencode
根据普通的百分比编码(空格用 编码%20
)进行编码。
回答by Oliver Charlesworth
The cunningly-named urlencode()and urldecode().
巧妙命名的urlencode()和urldecode()。
However, you shouldn't need to use urldecode()
on variables that appear in $_POST
and $_GET
.
但是,您不需要对urldecode()
出现在$_POST
和 中的变量使用$_GET
。
回答by William Entriken
Here is my use case, which requires an exceptional amount of encoding. Maybe you think it contrived, but we run this on production. Coincidently, this covers every type of encoding, so I'm posting as a tutorial.
这是我的用例,它需要大量的编码。也许你认为它是人为的,但我们在生产中运行它。巧合的是,这涵盖了所有类型的编码,因此我将其作为教程发布。
Use case description
用例描述
Somebody just bought a prepaid gift card ("token") on our website. Tokens have corresponding URLs to redeem them. This customer wants to email the URL to someone else. Our web page includes a mailto
link that lets them do that.
有人刚刚在我们的网站上购买了预付礼品卡(“代币”)。令牌有相应的 URL 来兑换它们。该客户希望通过电子邮件将该 URL 发送给其他人。我们的网页包含一个mailto
链接,可以让他们这样做。
PHP code
PHP代码
// The order system generates some opaque token
$token = 'w%a&!e#"^2(^@azW';
// Here is a URL to redeem that token
$redeemUrl = 'https://httpbin.org/get?token=' . urlencode($token);
// Actual contents we want for the email
$subject = 'I just bought this for you';
$body = 'Please enter your shipping details here: ' . $redeemUrl;
// A URI for the email as prescribed
$mailToUri = 'mailto:?subject=' . rawurlencode($subject) . '&body=' . rawurlencode($body);
// Print an HTML element with that mailto link
echo '<a href="' . htmlspecialchars($mailToUri) . '">Email your friend</a>';
Note: the above assumes you are outputting to a text/html
document. If your output media type is text/json
then simply use $retval['url'] = $mailToUri;
because output encoding is handled by json_encode()
.
注意:以上假设您正在输出到text/html
文档。如果您的输出媒体类型text/json
只是使用,$retval['url'] = $mailToUri;
因为输出编码由json_encode()
.
Test case
测试用例
- Run the code on a PHP test site (is there a canonical one I should mention here?)
- Click the link
- Send the email
- Get the email
- Click that link
- 在 PHP 测试站点上运行代码(我应该在这里提到一个规范的站点吗?)
- 点击链接
- 发送电子邮件
- 获取电子邮件
- 点击那个链接
You should see:
你应该看到:
"args": {
"token": "w%a&!e#\"^2(^@azW"
},
And of course this is the JSON representation of $token
above.
当然,这是$token
上面的 JSON 表示。
回答by Amranur Rahman
You can use URL Encoding Functions PHP has the
您可以使用 URL 编码函数 PHP 具有
rawurlencode()
function
功能
ASP has the
ASP 具有
Server.URLEncode()
function
功能
In JavaScript you can use the
在 JavaScript 中,您可以使用
encodeURIComponent()
function.
功能。