URL解码在PHP

问题描述:

我试图使用PHP的urldecode函数这个URL字符串解码:URL解码在PHP

urldecode("Ant%C3%B4nio+Carlos+Jobim"); 

据说这是为了输出...

'Antônio Carlos Jobim' 

...而是被ouptutting这

'Antônio Carlos Jobim' 

我测试过的非常成功JS-based online decoder字符串,但似乎无法做到这一点operati在服务器端。有任何想法吗?

+1

什么方法您使用的输出呢? – Kazar 2009-11-18 15:47:14

您的字符串是也是 UTF-8编码。这将工作:

echo utf8_decode(urldecode("Ant%C3%B4nio+Carlos+Jobim")); 

产量:“AntônioCarlos Jobim”。

+4

只有页面声明为“ISO-8859-1”编码。 – Kornel 2009-11-26 12:58:30

实际上,您会得到所需的输出,但它不会被解释为UTF-8。如果这是在HTTP应用程序上,您应该发送一个头部或元标记(或两者),这会告诉客户端使用UTF-8。

编辑:例如:

// replace text/html with the content type you're using 
header('Content-Type: text/html; charset=UTF-8'); 

你也呼应它的页面之前使用htmlenteties?当我刚刚测试你的代码时,它只能运行urldecode("Ant%C3%B4nio+Carlos+Jobim");部分,但是当我通过htmlentities运行时,我得到的输出和你一样。

这似乎是UTF-8字符的问题,以及PHP如何处理htmlentities函数。

+1

如果您指定正确的编码为'$ charset'参数,它将正常工作。无论如何,如果你想做的只是防止XSS,你应该使用'htmlspecialchars',而不是'htmlentities'。 – 2009-11-18 15:48:55

当我做

<?php 
echo urldecode("Ant%C3%B4nio+Carlos+Jobim"); 
?> 

其正确显示在我的浏览器,如

安东尼奥·卡洛斯·裘宾

我与XAMPP

测试的另一种选择是:

<?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; 
?> 

输出变为:http://mysite.com