PHP 字符集重音问题

发布于 2024-11-02 02:56:19 字数 509 浏览 4 评论 0原文

我的页面中有一个表单供用户发表评论。

我当前正在使用此字符集:

meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1" 

但从数据库重音检索注释未显示正确( Ex. è =>è )。

为了正确处理重音,我应该关心哪些参数?

已解决

  • 将元标记更改为 charset='utf-8'

  • 更改了 Mysql 的字符集(ALTER TABLE comments CONVERT TO CHARACTER SET utf-8)

  • 更改了连接字符集插入记录和检索时 ($conn->query('SET NAMES utf8'))

现在口音显示正确

谢谢卢卡

I have a form in my page for users to leave a comment.

I'm currently using this charset:

meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1" 

but retrieveving the comment from DB accents are not displaying correct ( Ex. è =>è ).

Which parameters should i care about for a correct handling of accents?

SOLVED

  • changed meta tag to charset='utf-8'

  • changed character-set Mysql (ALTER TABLE comments CONVERT TO CHARACTER SET utf-8)

  • changed connection character-set both when inserting records and retrieving ($conn->query('SET NAMES utf8'))

Now accents are displaying correct

thanks

Luca

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昨迟人 2024-11-09 02:56:19

当涉及到 LAMP Web 应用程序时,字符集可能很复杂并且调试起来很痛苦。在一个软件与另一个软件对话的每个阶段,都可能存在错误的字符集转换或错误的数据存储。

您需要注意的地方是:
- 在浏览器和网络服务器之间(您已经列出)
- PHP 和 MySQL 服务器之间

您列出的字符看起来像普通的欧洲字符,该字符将包含在 ISO-8859-1 字符集中。

要检查的事项:

  • 即使您在元标头中指定了字符集,也要在浏览器中查看以确保浏览器实际使用的是哪个字符集。如果您已指定它,浏览器应使用该字符集来呈现/查看页面,但在某些情况下,我看到它尝试自动检测正确的字符集并失败。大多数浏览器都有一个“编码”菜单(可能在“视图”下),允许您选择字符集。确保标有 ISO-8859-1(西欧)。
  • 如果需要,MySQL 可以很乐意支持字符集转换,但在大多数情况下,您希望表和客户端连接集使用相同的编码。当以这种方式配置时,MySQL 不会尝试进行任何编码转换,而只会将您输入的数据逐字节写入表中。读取时,它会以相同的方式逐字节输出。

您没有说您是否使用同一个网络应用程序或其他客户端从数据库中读取数据。我建议您尝试使用相同的网络应用程序并使用相同的元字符集标头来读取它(再次检查浏览器是否确实设置了它)并查看浏览器中显示的内容。

要调试这些问题,需要您真正确定您正在使用的客户端/控制台是否也在进行任何转换,最安全的方法有时是将数据输入十六进制编辑器,在其中您可以确保没有其他任何事情乱七八糟与任何翻译。

如果它看起来不像是浏览器端问题,请您在数据库中包含以下命令的输出:

从您的 Web 应用程序建立的连接运行(而不是从其他 MySQL 客户端):

SHOW VARIABLES LIKE 'character_set%';
SHOW VARIABLES LIKE 'collation%';

从任何 MySQL 运行客户端:(

SHOW CREATE TABLE myTable;

其中 myTable 是您正在从中读取/写入数据的表)

Character sets can be complicated and pain to debug when it comes to LAMP web applications. At each of the stages that one piece of software talks to another there's scope for incorrect charset translation or incorrect storage of data.

The places you need to look out for are:
- Between the browser and the web server (which you've listed already)
- Between PHP and the MySQL server

The character you've listed look like normal a European character that will be included in the ISO-8859-1 charset.

Things to check for:

  • even though you're specifying the character set in a meta header have a look in your browser to be sure which character set the browser is actually using. If you've specified it the browser should use that charset to render/view the page but in cases I've seen it attempting to auto-detect the correct charset and failing. Most browsers will have an "encoding" menu (perhaps under "view") that allows you to choose the charset. Ensure that it says ISO-8859-1 (Western European).
  • MySQL can happily support character set conversion if required to but in most cases you want to have your tables and client connection set to use the same encoding. When configured this way MySQL won't attempt to do any encoding conversion and will just write the data you input byte for byte into the table. When read it'll come out the same way byte for byte.

You've not said if you're reading data from the database back out with the same web-app or with some other client. I'd suggest you try to read it out with the same web application and using the same meta charset header (again, check the browser is really setting it) and see what is displayed in the browser.

To debug these issues requires you to be really sure about whether the client/console you're using is doing any conversion too, the safest way is sometimes to get the data into a hex editor where you can be sure that nothing else is messing around with any translation.

If it doesn't look like it's a browser-side problem please can you include the output of the following commands against your database:

Run from a connection that your web-app makes (not from some other MySQL client):

SHOW VARIABLES LIKE 'character_set%';
SHOW VARIABLES LIKE 'collation%';

Run from any MySQL client:

SHOW CREATE TABLE myTable;

(where myTable is the table you're reading/writing data from/to)

永不分离 2024-11-09 02:56:19

ISO-8859-1 字符集仅适用于拉丁字符。尝试使用 UTF-8,并确保这些字符来自的数据库也是 UTF-8 列。

The ISO-8859-1 character set is for Latin characters only. Try UTF-8, and make sure that the database these characters are coming from are also UTF-8 columns.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文