使用 utf8_encode()(php 函数)的国际化内容最佳实践

发布于 2024-12-03 19:00:28 字数 301 浏览 2 评论 0原文

为了让网站接受用户提交的可能不是英语(例如日语)的内容并将其保存到数据库中,对所有新内容进行 utf8_encode 并在稍后检索时使用 utf8_decode 是否符合我的最佳利益?

更多信息: 我正在使用原则,并且在尝试将 Unicode 字符保存或选择到 MySQL 数据库时遇到错误:

SQLSTATE[HY000]:一般错误:1267 操作“=”的排序规则 (latin1_swedish_ci,IMPLICIT) 和 (utf8_general_ci,COERCIBLE) 的非法混合

In order for website to accept user submitted content which may not be in English (e.g. Japanese) and save it to the database, is it in my best interest to utf8_encode all new content, and user utf8_decode when retrieving it later?

Further info:
I am using doctrine and I am getting an errors when attempting to save or select Unicode characters to the MySQL database:

SQLSTATE[HY000]: General error: 1267 Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation '='

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

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

发布评论

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

评论(2

小鸟爱天空丶 2024-12-10 19:00:28

您不需要使用编码功能。您需要做的是确保端到端都是 UTF8。看起来您的数据库可能正在使用 latin1 编码和排序规则。您与数据库的连接也需要是 UTF8。有时,只需在建立连接后立即执行 SET NAMES UTF8 查询即可。

在 mysql 中运行此命令可能会解决您在上面看到的错误,但您仍然需要端到端 UTF8。那么您无需对数据执行任何特殊操作。

ALTER TABLE table_name CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;

You don't need to use the encode function. What you need to do is make sure you are UTF8 end to end. Looks like you database might be using latin1 encoding and collation. Your connection to the database also needs to be UTF8. Sometimes that simply a matter of executing SET NAMES UTF8 query right after you establish a connection.

Running this command in mysql will likely resolve the error you see above, but you still need to be end-to-end UTF8. Then you don't need to do anything special with your data.

ALTER TABLE table_name CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
铃予 2024-12-10 19:00:28

布伦特是对的。它需要是端到端的。这是我的清单:

Apache config:
    AddDefaultCharset UTF-8
    AddCharset UTF-8  .utf8

php.ini:
    default_charset = "utf-8"

MySQL:
    ALTER DATABASE DEFAULT CHARACTER SET utf8;
    ALTER TABLE SomeTableName DEFAULT CHARACTER SET utf8;

PHP/HTML:
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    …
    <form … <input type="text" name="some_field" value="<?php echo htmlspecialchars($row['some_field'], ENT_COMPAT, 'UTF-8'); ?>"…

This last one seems the most important. Call this function immediately after the mysql_connect() call:
    mysql_query("SET NAMES 'utf8'");

Brent is right. It needs to be end-to-end. Here's my list:

Apache config:
    AddDefaultCharset UTF-8
    AddCharset UTF-8  .utf8

php.ini:
    default_charset = "utf-8"

MySQL:
    ALTER DATABASE DEFAULT CHARACTER SET utf8;
    ALTER TABLE SomeTableName DEFAULT CHARACTER SET utf8;

PHP/HTML:
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    …
    <form … <input type="text" name="some_field" value="<?php echo htmlspecialchars($row['some_field'], ENT_COMPAT, 'UTF-8'); ?>"…

This last one seems the most important. Call this function immediately after the mysql_connect() call:
    mysql_query("SET NAMES 'utf8'");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文