【问题标题】:URL Decoding in PHPPHP中的URL解码
【发布时间】:2019-01-03 17:33:11
【问题描述】:

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

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

这应该是输出...

'Antônio Carlos Jobim'

...而是输出这个

'Antônio Carlos Jobim'

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

【问题讨论】:

  • 你是用什么方法输出的?

标签: php url utf-8 urldecode


【解决方案1】:

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

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

输出:“Antônio Carlos Jobim”。

【讨论】:

  • 仅当页面声明 ISO-8859-1 编码时。
【解决方案2】:

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

编辑: 例如:

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

【讨论】:

    【解决方案3】:

    当我这样做时

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

    它在我的浏览器中正确显示

    安东尼奥·卡洛斯·约宾

    我已经用 XAMPP 测试过

    【讨论】:

      【解决方案4】:

      另一种选择是:

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

      【讨论】:

      • 这行得通。只有 urldecode() 不会使用查询参数解码整个 URL。问题是您的解决方案考虑的时间太长:/
      【解决方案5】:

      在将htmlenteties 回显到页面之前,您是否也在使用它?当我刚刚测试你的代码时,它只使用urldecode("Ant%C3%B4nio+Carlos+Jobim"); 部分就可以正常工作,但是当我通过htmlentities 运行它时,我得到了和你一样的输出。

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

      【讨论】:

      • 如果您将正确的编码指定为$charset 参数,它可以正常工作。无论如何,如果你想做的只是防止 XSS,你应该使用htmlspecialchars,而不是htmlentities
      【解决方案6】:

      首先你必须在 urldecoder 函数“urldecoder()”中解码,然后使用 utf解码器函数“utf_decoder()”

      【讨论】:

        猜你喜欢
        • 2011-08-05
        • 2012-11-27
        • 1970-01-01
        • 2011-11-09
        • 2017-05-31
        • 1970-01-01
        • 2013-10-10
        • 1970-01-01
        • 2013-01-14
        相关资源
        最近更新 更多