使用php 5.5的utf-8编码不适用于utf-8编码的文档 [英] Utf-8 encoding not working on utf-8 encoded document using php 5.5

查看:77
本文介绍了使用php 5.5的utf-8编码不适用于utf-8编码的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一种非常奇怪的编码问题,我不太了解,也从未遇到过.我仅在Ubuntu机器上使用PHP 5.5来获取信息.

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.

解决问题

我有一个简单的文件index.php,我想在其中打印这个简单的字符串

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

这像预期的那样工作!

要以正确的方式显示此信息,请按照以下步骤操作

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

  1. 将我的IDE(Zend Studio)的编码更改为UTF-8并再次保存文件
  2. 设置适当的html元标记

  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" />

  • 设置适当的php标头

  • 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!

    解决方法

    要使此东西正确显示,我必须这样做

    To make this thing displaying correctly i had to do that

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

    现在它以正确的方式显示.

    Now it displays in a correct way.

    我的问题

    我真的不明白为什么我的文档已经被编码并保存在utf-8中时为什么必须使用 utf8_encode 的原因.这对我来说没有任何意义.有什么解释吗?

    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?

    推荐答案

    没有答案,但是评论太久了:

    Not an answer but too long for a comment:

    可以请您尝试

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

    在您拥有<?php echo "übermotivierter"; ?>的地方吗?
    它的输出是什么?

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

    这篇关于使用php 5.5的utf-8编码不适用于utf-8编码的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆