来自MySQL数据库的字符的UTF-8问题(例如é作为é) [英] UTF-8 problems with characters from MySQL database (e.g. é as é)

查看:200
本文介绍了来自MySQL数据库的字符的UTF-8问题(例如é作为é)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有关UTF-8问题的数百个问题,但我尝试了所有可以找到的方法,但没有一个有帮助.

I know there are hundreds of questions about UTF-8 woes but I tried all the approaches I could find, none of them helped.

事实: 我正在尝试从MySQL数据库中读取包含é的字符串,并将其显示在PHP页面上.实际上,它的确显示为é(但字体不能这样识别,因此使用了另一种默认字体).当我想使用PHP函数将字符串转换为文件名来替换字符串时,出现了麻烦. PHP根本不认为这是é字符.

The facts: I'm trying to read a string that contains a é from my MySQL database and display it on a PHP page. Actually, it does display as é (but the font does not recognize it as such and thus another default font is used). The troubles arose when I wanted to convert this string to a filename using PHP functions for string replacement. PHP does not recognize this as the é character at all.

以下是我正在做的事情的简要介绍:

Here's a quick rundown of what I'm doing:

1)字符串存储在MySQL数据库中. MySQL服务器设置为: MySQL连接整理utf8_unicode_ci
MySQL字元集:UTF-8 Unicode(utf8)
数据库本身设置为排序规则utf8_unicode_ci(MyISAM存储引擎,由于共享服务器而无法更改)
实际表设置为collcation utf8_unicode_ci(InnoDB存储引擎)

1) The String is stored in a MySQL database. The MySQL server settings are: MySQL connection collation utf8_unicode_ci
MySQL charset: UTF-8 Unicode (utf8)
The database itself is set to collation utf8_unicode_ci (MyISAM storage engine, not changeable due to shared server)
The actual table is set to collcation utf8_unicode_ci (InnoDB storage engine)

é在phpMyAdmin中正确显示.数据是通过Java程序插入数据库的,但我也尝试过使用手动输入的数据(在phpMyAdmin中输入)进行此操作.

The é shows up correctly in phpMyAdmin. The data is inserted into the DB via a Java program but I have also tried this with manually entered data (entered in phpMyAdmin).

2)未设置PHP default_charset(无值),我在共享服务器上,放置手动替代php.ini似乎不起作用.使用ini_set("default_charset", 'utf-8');可行,但是对我遇到的问题没有影响.

2) The PHP default_charset is not set (NO VALUE), I'm on a shared server and placing a manual override php.ini did not seem to work. Using ini_set("default_charset", 'utf-8'); works but has no effect on the problem I have.

3)在运行实际的选择查询之前,我先查询SET NAMES 'utf8'.查询本身无关紧要,但是为了进行测试,我选择了一个简单的SELECT title FROM items WHERE item_id = 1

3) Before I run the actual select query I query SET NAMES 'utf8'. The query itself is irrelevant but for testing I chose a simple SELECT title FROM items WHERE item_id = 1

4)PHP文件本身已编码为UTF-8.我已经使用<meta http-equiv="content-type" content="text/html; charset=utf-8" />

4) The PHP file itself is encoded UTF-8. I have set the correct charset for the html with <meta http-equiv="content-type" content="text/html; charset=utf-8" />

5)为了测试问题,我在返回的字符串(Astérix)上使用了htmlentities,检查将其转换为Ast&Atilde;&copy;rix的源代码,这当然是不正确的.因此,该字符串在浏览器中显示为Astérix.

5) To test the problem I used htmlentities on the returned string (Astérix), checking the source code it is converted to Ast&Atilde;&copy;rix which is not correct of course. Accordingly, the string shows up as Astérix in the browser.

这可能是什么原因?对我来说,似乎我将所有可以设置为UTF-8的内容都设置了.

What possible reason could there be for this? To me it seems like I set everything that can be set to UTF-8.

推荐答案

http://php.net/manual/en/ref.mbstring.php -查看多字节字符串函数.

http://php.net/manual/en/ref.mbstring.php - look at multibyte string functions.

这篇关于来自MySQL数据库的字符的UTF-8问题(例如é作为é)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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