php mysql字符集:存储国际内容的html [英] php mysql character set: storing html of international content

查看:169
本文介绍了php mysql字符集:存储国际内容的html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我完全困惑于我读过的字符集。我正在开发一个界面来存储格式化在html里面的法语文本到mysql数据库中。

i'm completely confused by what i've read about character sets. I'm developing an interface to store french text formatted in html inside a mysql database.

我理解的是,使所有法语特殊字符正常显示的安全方法将它们存储为utf8。所以我创建了一个mysql数据库,utf8为数据库和每个表指定。
我可以通过phpmyadmin看到字符的存储方式与它应该是一样的。但通过php输出这些字符给我不规则的结果:重音字符被无意义的字符替换。为什么呢?

What i understood was that the safe way to have all french special characters displayed properly would be to store them as utf8. so i've created a mysql database with utf8 specified for the database and each table. I can see through phpmyadmin that the characters are stored exactly the way it is supposed to. But outputting these characters via php gives me erratic results: accented characters are replaced by meaningless characters. Why is that ?

我需要utf8_encode或utf8_decode?注意:html页面的字符编码设置为utf8。

do i have to utf8_encode or utf8_decode them? note: the html page character encodign is set to utf8.

一般来说,存储这个数据的安全方法是什么?我应该在保存时结合htmlentities,addslashes和utf8_encode,当我输出时,结合striplashes,html_entity_decode和utf8_decode?

more generally, what is the safe way to store this data? Should i combine htmlentities, addslashes, and utf8_encode when saving, and stripslashes,html_entity_decode and utf8_decode when i output?

推荐答案

将即时转换设为连接字符集。您可以使用sql语句指定此字符集

MySQL performs character set conversions on the fly to something called the connection charset. You can specify this charset using the sql statement

SET NAMES utf8

或使用特定的API函数,例如 mysql_set_charset()

or use a specific API function such as mysql_set_charset():

mysql_set_charset("utf8", $conn);

如果这样做正确无需使用诸如utf8_encode()和utf8_decode

If this is done correctly there's no need to use functions such as utf8_encode() and utf8_decode().

您还必须确保浏览器使用相同的编码。这通常使用一个简单的头:

You also have to make sure that the browser uses the same encoding. This is usually done using a simple header:

header('Content-type: text/html;charset=utf-8');

(请注意,浏览器中的字符集称为 utf-8

(Note that the charset is called utf-8 in the browser but utf8 in MySQL.)

在大多数情况下,连接字符集和网络字符集是唯一需要跟踪的事情,所以如果它仍然不工作可能是别的你做错了。尝试尝试一下,通常需要一段时间才能完全理解。

In most cases the connection charset and web charset are the only things that you need to keep track of, so if it still doesn't work there's probably something else your doing wrong. Try experimenting with it a bit, it usually takes a while to fully understand.

这篇关于php mysql字符集:存储国际内容的html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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