PHP中的URL解码

时间:2009-11-18 15:39:34

标签: php url utf-8 urldecode

我正在尝试使用PHP的urldecode函数解码此URL字符串:

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

这应该输出......

'Antônio Carlos Jobim'

...而是将这个

ou
'Antônio Carlos Jobim'

我已经在JS-based online decoder中测试了字符串并取得了巨大成功,但似乎无法在服务器端执行此操作。有什么想法吗?

6 个答案:

答案 0 :(得分:62)

您的字符串 UTF-8编码。这将有效:

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

输出:“AntônioCarlosJobim”。

答案 1 :(得分:13)

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

修改,例如:

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

答案 2 :(得分:2)

当我做的时候

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

在我的浏览器中正确显示

  

AntônioCarlosJobim

我已经使用XAMPP进行了测试

答案 3 :(得分:1)

您是否也在使用htmlenteties之前将其回显到页面?当我刚刚测试你的代码时,它只适用于urldecode("Ant%C3%B4nio+Carlos+Jobim");部分,但是当我通过htmlentities运行它时,我得到了与你相同的输出。

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

答案 4 :(得分:0)

另一种选择是:

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

答案 5 :(得分:-1)

首先,您必须在urldecoder函数“ urldecoder()”中进行解码,然后使用 utf解码器功能“ utf_decoder()”