PHP 中的 URL 解码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1756862/
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
URL Decoding in PHP
提问by Brandon Hymanson
I am trying to decode this URL string using PHP's urldecode function:
我正在尝试使用 PHP 的 urldecode 函数解码这个 URL 字符串:
urldecode("Ant%C3%B4nio+Carlos+Jobim");
This is supposed to output...
这应该输出...
'Ant?nio Carlos Jobim'
...but instead is ouptutting this
...但相反的是输出这个
'Ant?′nio Carlos Jobim'
I've tested the string in a JS-based online decoderwith great success, but can't seem to do this operation server side. Any ideas?
我已经在基于JS 的在线解码器中测试了该字符串并取得了巨大成功,但似乎无法在服务器端执行此操作。有任何想法吗?
回答by Kristoffer Bohmann
Your string is alsoUTF-8 encoded. This will work:
您的字符串也是UTF-8 编码的。这将起作用:
echo utf8_decode(urldecode("Ant%C3%B4nio+Carlos+Jobim"));
Output: "Ant?nio Carlos Jobim".
输出:“Ant?nio Carlos Jobim”。
回答by Ignas R
Actually, you get the desired output, but it is not interpreted as UTF-8. If this is on an HTTP application, you should send a header or a meta tag (or both) which would tell the client to use UTF-8.
实际上,您获得了所需的输出,但它不会被解释为 UTF-8。如果这是在 HTTP 应用程序上,您应该发送一个标头或元标记(或两者),告诉客户端使用 UTF-8。
Edit:for example:
编辑:例如:
// replace text/html with the content type you're using
header('Content-Type: text/html; charset=UTF-8');
回答by YOU
when I do
当我做
<?php
echo urldecode("Ant%C3%B4nio+Carlos+Jobim");
?>
Its display correctly in my browser like
它在我的浏览器中正确显示
Ant?nio Carlos Jobim
安东尼奥·卡洛斯·乔宾
I have tested with XAMPP
我已经用 XAMPP 测试过了
回答by Doug Neiner
Are you also using htmlentetiesbefore echoing it to the page? When I just tested your code it worked fine with just the urldecode("Ant%C3%B4nio+Carlos+Jobim");part, but when I ran it through htmlentitiesI got the same output as you did.
您是否也在将其htmlenteties回显到页面之前使用?当我刚刚测试您的代码时,它只对urldecode("Ant%C3%B4nio+Carlos+Jobim");部分工作正常,但是当我运行它时,htmlentities我得到了与您相同的输出。
It seems to be a problem with the UTF-8 characters and how PHP handles the htmlentitiesfunction.
这似乎是 UTF-8 字符以及 PHP 如何处理该htmlentities函数的问题。
回答by T.Todua
another option is:
另一种选择是:
<?php
$smthing = 'http%3A%2F%2Fmysite.com';
$smthing = preg_replace("/%u([0-9a-f]{3,4})/i","&#x\1;",urldecode($smthing));
$smthing = html_entity_decode($smthing,null,'UTF-8');
echo $smthing;
?>
output becomes : http://mysite.com
输出变为: http://mysite.com
回答by Keshav Sinha
first you have to decode in urldecoder function "urldecoder()" and then use utf decoder function "utf_decoder()"
首先你必须在 urldecoder 函数“urldecoder()”中解码,然后使用 utf 解码器函数“utf_decoder()”

