【问题标题】:Force encoding in utf-8 for a php page强制在 utf-8 中对 php 页面进行编码
【发布时间】:2012-06-01 10:28:07
【问题描述】:

我有一个 VPS(Debian、Apache、MySQL、PHP)

我想强制使用 UTF-8 编码。我已经把这条线:

header('Content-type: application/json; charset: UTF-8');

但字符集仍然是 ISO8859

我还编辑了 php.ini:/etc/php5/apache2/php.ini

mbstring.language=UTF-8
mbstring.internal_encoding= UTF-8
mbstring.http_input=UTF-8
mbstring.http_output=UTF-8
mbstring.detect_order= auto

和 apache conf:

nano /etc/apache2/conf.d/charset
AddDefaultCharset UTF-8

一个 iOS 应用程序正在调用我的 php 页面。我给它一个 JSON 输出。但它接收 ISO 字符集...

【问题讨论】:

  • 当你说“字符集仍然是 ISO8859”时,你是什么意思?你在哪里看到这个价值?
  • 我查看页面信息(Firefox 中的右键),编码是 ISO。我有一个 json 输出。

标签: php apache encoding utf-8 debian


【解决方案1】:

在 .htaccess 文件中可以放:

php_value default_charset UTF-8

【讨论】:

    【解决方案2】:

    您的响应标头不正确:

    header('Content-type: application/json; charset: UTF-8');
    

    应该是:

    header('Content-type: application/json; charset=UTF-8');
    

    注意= 代替:

    【讨论】:

      【解决方案3】:

      你看过utf8-encode函数:http://php.net/manual/en/function.utf8-encode.php吗?

      您可能正在向客户端回显未正确编码的文本。 Apache 不会为您执行此操作。

      【讨论】:

      • 是的,但是当我查看页面信息(Firefox 中的右键)时,编码是 ISO
      • 您是否检查过是否有任何警告消息可以在错误控制台中提供帮助?
      • 哦。想通了。您的标头错误,您应该使用Content-type: application/json; charset=UTF-8 而不是Content-type: application/json; charset: UTF-8
      猜你喜欢
      • 2011-08-19
      • 2016-10-06
      • 1970-01-01
      • 2017-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多