Utf-8字符显示为ISO-8859-1 [英] Utf-8 characters displayed as ISO-8859-1

查看:256
本文介绍了Utf-8字符显示为ISO-8859-1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从数据库插入/读取utf8内容时出现问题.我正在做的所有验证似乎都指向一个事实,即我数据库中的内容应该是utf8编码的,但是它似乎是拉丁编码的.数据最初是从CLI的PHP脚本导入的.

配置:

Zend Framework Version: 1.10.5
mysql-server-5.0:   5.0.51a-3ubuntu5.7
php5-mysql:     5.2.4-2ubuntu5.10
apache2:        2.2.8-1ubuntu0.16
libapache2-mod-php5:    5.2.4-2ubuntu5.10

证明:

-mysql:

mysql> SHOW VARIABLES LIKE 'character_set%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

mysql> SHOW VARIABLES LIKE 'collation%';
+----------------------+-----------------+
| Variable_name        | Value           |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database   | utf8_bin        |
| collation_server     | utf8_general_ci |
+----------------------+-----------------+

-数据库

created with 
CREATE DATABASE mydb CHARACTER SET utf8 COLLATE utf8_bin;
CREATE SCHEMA `mydb` DEFAULT CHARACTER SET utf8 COLLATE utf8_bin ;

mysql> status;
--------------
mysql  Ver 14.12 Distrib 5.0.51a, for debian-linux-gnu (i486) using readline 5.2

Connection id:          7
Current database:       mydb
Current user:           root@localhost
SSL:                    Not in use
Current pager:          stdout
Using outfile:          ''
Using delimiter:        ;
Server version:         5.0.51a-3ubuntu5.7-log (Ubuntu)
Protocol version:       10
Connection:             Localhost via UNIX socket
Server characterset:    utf8
Db     characterset:    utf8
Client characterset:    utf8
Conn.  characterset:    utf8
UNIX socket:            /var/run/mysqld/mysqld.sock
Uptime:                 9 min 45 sec

-sql:在执行插入操作之前,先运行

SET names 'utf8';

-php:在执行插入操作之前,我使用 utf8_encode() mb_detect_encoding(),这给了我'UTF-8'.从db检索内容并将其发送给用户mb_detect_encoding()之前,它还会提供'UTF-8'

验证测试:

让内容正确显示的唯一方法是将内容类型设置为拉丁文(如果我监听流量,则可以看到ISO-8859-1的内容类型标头):

ini_set('default_charset', 'ISO-8859-1');

此测试表明内容以拉丁语出现.我不明白为什么. 有人有什么主意吗?

谢谢.

解决方案

好吧,我发现SET NAMES并不是真的很棒.在文档 ...

我通常要做的是执行4个查询:

SET CHARACTER SET 'UTF8';
SET character_set_database = 'UTF8';
SET character_set_connection = 'UTF8';
SET character_set_server = 'UTF8';

给个镜头,看看能不能帮到你...

哦,请记住,所有< = 127的UTF-8字符也是有效的ISO-8859-1字符.因此,如果流中只有< = 127个字符,则mb_detect_encoding将落在较高流行性的字符集上(默认情况下为"UTF-8")...

I've got an issue with inserting/reading utf8 content from a db. All verifications I'm doing seem to point to the fact that the content in my DB should be utf8 encoded, however it seems to be latin encoded. The data are initially imported from a PHP script from the CLI.

Configuration:

Zend Framework Version: 1.10.5
mysql-server-5.0:   5.0.51a-3ubuntu5.7
php5-mysql:     5.2.4-2ubuntu5.10
apache2:        2.2.8-1ubuntu0.16
libapache2-mod-php5:    5.2.4-2ubuntu5.10

Vertifications:

-mysql:

mysql> SHOW VARIABLES LIKE 'character_set%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

mysql> SHOW VARIABLES LIKE 'collation%';
+----------------------+-----------------+
| Variable_name        | Value           |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database   | utf8_bin        |
| collation_server     | utf8_general_ci |
+----------------------+-----------------+

-database

created with 
CREATE DATABASE mydb CHARACTER SET utf8 COLLATE utf8_bin;
CREATE SCHEMA `mydb` DEFAULT CHARACTER SET utf8 COLLATE utf8_bin ;

mysql> status;
--------------
mysql  Ver 14.12 Distrib 5.0.51a, for debian-linux-gnu (i486) using readline 5.2

Connection id:          7
Current database:       mydb
Current user:           root@localhost
SSL:                    Not in use
Current pager:          stdout
Using outfile:          ''
Using delimiter:        ;
Server version:         5.0.51a-3ubuntu5.7-log (Ubuntu)
Protocol version:       10
Connection:             Localhost via UNIX socket
Server characterset:    utf8
Db     characterset:    utf8
Client characterset:    utf8
Conn.  characterset:    utf8
UNIX socket:            /var/run/mysqld/mysqld.sock
Uptime:                 9 min 45 sec

-sql: before doing my inserts I run the

SET names 'utf8';

-php: before doing my inserts I use utf8_encode() and mb_detect_encoding() which gives me 'UTF-8'. After retrieveing the content from db and before sending it to the user mb_detect_encoding() also gives 'UTF-8'

Validation test:

the only way for me to have the content displayed properly is to set the content type to latin (If I sniff the traffic I can see the content-type header with ISO-8859-1):

ini_set('default_charset', 'ISO-8859-1');

This test shows that the content comes out as latin. I don't understand why. Does anybody have any idea?

Thanks.

解决方案

Well, I've found that SET NAMES isn't really all that great. Take a peak at the docs...

What I typically do is execute 4 queries:

SET CHARACTER SET 'UTF8';
SET character_set_database = 'UTF8';
SET character_set_connection = 'UTF8';
SET character_set_server = 'UTF8';

Give that a shot and see if that does it for you...

Oh, and remember, all UTF-8 characters <= 127 are valid ISO-8859-1 characters as well. So if you only have characters <= 127 in the stream, mb_detect_encoding will fall on the higher prevalence charset (which is by default "UTF-8")...

这篇关于Utf-8字符显示为ISO-8859-1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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