doushan15559 2015-07-16 08: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 08: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条)

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类