doushan15559 2015-07-16 00:14
浏览 73
已采纳

Utf-8编码不能使用php 5.5在utf-8编码的文件上工作

I am experiencing a very strange kind of encoding problem i don't really understand and never had before. I am using PHP 5.5 on an Ubuntu machine just for the info.

To the Problem

I have a simple file index.php where i want to print this simple string

<?php echo "übermotivierter";  ?>

When viewing this in the Browser i would expect following ouput

�bermotivierter

This works like expected!

To display this in the correct way i have done following steps

  1. Changed the encoding of my IDE ( Zend Studio ) to UTF-8 and saved the file again
  2. Set the appropriate html meta tag

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
  3. Set the appropriate php header

    <?php header("Content-Type: text/html; charset=utf-8"); ?>
    

After doing this i would have expected this to display in a correct way but i am still getting this weired � in the output!

The workaround

To make this thing displaying correctly i had to do that

<?php echo utf8_encode("übermotivierter");  ?>

Now it displays in a correct way.

My Question

I really dont understand why i have to use utf8_encode when my document already is encoded and saved in utf-8. That doesn't make any sense to me. Any explanation to this?

  • 写回答

2条回答 默认 最新

  • duanju7199 2015-07-16 00:34
    关注

    Not an answer but too long for a comment:

    Could you please try

    <?php
    $s = "übermotivierter";
    echo '<p>', $s, '</p><p>';
    for($i=0; $i<strlen($s); $i++) {
        printf('%02x ', ord($s[$i]));
    }
    echo '</p>';
    

    in the place where you had <?php echo "übermotivierter"; ?>?
    What's the output of that?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部