dsgffz2579 2009-11-18 15:39
浏览 75
已采纳

PHP中的URL解码

I am trying to decode this URL string using PHP's urldecode function:

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 decoder with great success, but can't seem to do this operation server side. Any ideas?

  • 写回答

6条回答 默认 最新

  • du997562 2009-11-18 15:47
    关注

    Your string is also UTF-8 encoded. This will work:

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

    Output: "Antônio Carlos Jobim".

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • douba7784 2009-11-18 15:45
    关注

    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.

    Edit: for example:

    // replace text/html with the content type you're using
    header('Content-Type: text/html; charset=UTF-8');
    
    评论
  • dtihe8614 2009-11-18 15:45
    关注

    Are you also using htmlenteties before 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 htmlentities I got the same output as you did.

    It seems to be a problem with the UTF-8 characters and how PHP handles the htmlentities function.

    评论
  • donglu8779 2009-11-18 15:47
    关注

    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

    评论
  • douxuanling6523 2013-03-26 13:42
    关注

    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

    评论
  • doutongxuan1614 2019-01-03 09:33
    关注

    first you have to decode in urldecoder function "urldecoder()" and then use utf decoder function "utf_decoder()"

    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 k210显示failed init to model
  • ¥15 Evil-droid生成的APK手机已经下载但无法建立任务
  • ¥25 c语言韩信点兵的变式
  • ¥15 怎么根据书上的例子完成这个问题呢?
  • ¥15 ECharts 增加Zoom,整行包括右边的Text一起滑动
  • ¥15 关于网上一个easyx制作的见缝插针小游戏(c++)
  • ¥15 开地址法双散列函数处理碰撞
  • ¥15 想问一下这个是什么情况 虚拟机Linux打不开了
  • ¥15 联通光猫掉注册了怎么重新注册上去
  • ¥15 关于unity开发steamvr程序遇到的问题