我正在创建一个搜索页面,您可以在其中键入搜索查询并将表单提交给search.php?query=your query.什么PHP函数是最好的,我应该用于编码/解码搜索查询?
Gum*_*mbo 171
对于URI查询使用urlencode/ urldecode; 其他任何使用rawurlencode/ rawurldecode.
urlencode和之间的区别rawurlencode是
urlencode根据应用程序编码/ x-www-form-urlencoded(空格编码+)whilerawurlencode根据普通的Percent-Encoding进行编码(空间用编码%20).Oli*_*rth 20
狡猾命名的urlencode()和urldecode().
但是,您不应该使用urldecode()出现在$_POST和中的变量$_GET.
这是我的用例,需要大量的编码.也许你认为它是人为的,但我们在生产中运行它.巧合的是,这涵盖了所有类型的编码,因此我将其作为教程发布.
有人刚在我们的网站上买了一张预付礼品卡("代币").令牌具有相应的URL以兑换它们.该客户希望通过电子邮件将其发送给其他人.我们的网页包含一个mailto链接,可以让他们这样做.
// 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>';
Run Code Online (Sandbox Code Playgroud)
注意:以上假定您输出到text/html文档.如果您的输出媒体类型text/json只是使用,$retval['url'] = $mailToUri;因为输出编码由处理json_encode().
你应该看到:
"args": {
"token": "w%a&!e#\"^2(^@azW"
},
Run Code Online (Sandbox Code Playgroud)
当然这是$token上面的JSON表示.