如何正确处理 PHP / MySQL / Apache 中的国际字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1728746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 18:23:54  来源:igfitidea点击:

How to properly handle international character in PHP / MySQL / Apache

phpmysqlapacheunicode

提问by agsamek

I need to create an application in PHP that can handle all Unicode characters in all places — edit fields, static HTML, database. Can somebody tell me the complete list of all parameters / functions that need to be set / used to achieve this goal?

我需要用 PHP 创建一个应用程序,它可以处理所有地方的所有 Unicode 字符——编辑字段、静态 HTML、数据库。有人可以告诉我需要设置/用于实现此目标的所有参数/功能的完整列表吗?

回答by Palantir

Apache

阿帕奇

The server encoding must be either not set, or set to UTF-8. This is done via the apache AddDefaultCharset directive. This can go to the virtualhost or the general file (see documentation).

服务器编码必须要么未设置,要么设置为 UTF-8。这是通过 apache AddDefaultCharset 指令完成的。这可以转到虚拟主机或通用文件(请参阅文档)。

AddDefaultCharset utf-8

MySql

数据库

  • Set the collation of the database to be UTF-8
  • Set the connection encoding. It can be done as someone said with mysqli_set_charset, or by sending this just after connecting:
  • 将数据库的排序规则设置为 UTF-8
  • 设置连接编码。可以像有人用 mysqli_set_charset 所说的那样完成,或者在连接后立即发送:
    SET NAMES 'utf8' COLLATE 'utf8_unicode_ci'

PHP

PHP

1- You should set the HTML charset of the page to be UTF-8, via a meta tag on the page, or via a PHP header:

1- 您应该通过页面上的元标记或通过 PHP 标头将页面的 HTML 字符集设置为 UTF-8:

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
-or-
    header('Content-type: text/html; charset=utf-8');

2- You should always use the mb* version of string-related functions, for example, mbstrlen instead of strlen to get the string length of a string.

2- 您应该始终使用字符串相关函数的 mb* 版本,例如, mbstrlen 而不是 strlen 来获取字符串的字符串长度。

This should allow you to have UTF-8 everywhere, from the pages to the data. A test you can do: right-click anywhere on the page using firefox, and select Show page information. The effective encoding is listed in that page.

这应该允许您在任何地方都使用 UTF-8,从页面到数据。您可以执行的测试:使用 firefox 右键单击​​页面上的任意位置,然后选择显示页面信息。有效编码列在该页面中。

回答by Pesse

Important: You should also ensure that you use UTF-8 as connection charset when connecting to Mysql from PHP!

重要提示:从 PHP 连接到 Mysql 时,您还应该确保使用 UTF-8 作为连接字符集!

For mysqli this is done by

对于 mysqli,这是由

mysqli_set_charset($dblink, 'utf-8')

http://de3.php.net/manual/en/mysqli.set-charset.php

http://de3.php.net/manual/en/mysqli.set-charset.php

回答by Rik Heywood

Some things you will need to look into:-

你需要研究的一些事情:-

PHP

PHP

Make sure your content is marked as utf-8 :

确保您的内容被标记为 utf-8 :

default_charset = "utf-8"

default_charset = "utf-8"

Install mbstring. You can find it here

安装 mbstring。你可以在这里找到

Ensure that you are talking utf-8 between PHP and MySQL.
Call mysql_set_charset("utf8");(or use the SQL query SET NAMES utf8)

确保您在 PHP 和 MySQL 之间使用 utf-8。
调用mysql_set_charset("utf8");(或使用 SQL 查询SET NAMES utf8

Apache

阿帕奇

You also set the Content-Type:of your pages in here with something like this

您还Content-Type:可以使用以下内容在此处设置页面

AddDefaultCharset utf-8

AddDefaultCharset utf-8

MySQL

MySQL

Make sure all your tables use utf8 Collation utf8_general_ci; eg

确保所有表都使用 utf8 Collat​​ion utf8_general_ci;例如

ALTER DATABASE mydb CHARACTER SET utf8;

Finally

最后

Finally, test stuff with fun unicode samples, like these ones

最后,用有趣的 unicode 样本测试东西,比如这些

?(?????)?

?(??????)?

More helpful information from when I tried this...

当我尝试这个时,更多有用的信息......

回答by TRiG

You were recommended to use either a HTTP header or a meta element to set the charset on your pages to utf-8. The W3C recommends that you do both. And the meta element should appear as early as possible on the page. (All characters before the meta element should be ASCII, which is basically identical in almost all character encodings. Some browsers will restart page rendering when they encounter the meta tag, which is another good reason to have it early.)

建议您使用 HTTP 标头或元元素将页面上的字符集设置为 utf-8。W3C 建议您同时进行。并且元元素应该尽可能早地出现在页面上。(meta 元素之前的所有字符都应该是 ASCII,这在几乎所有字符编码中都基本相同。有些浏览器在遇到 meta 标记时会重新启动页面渲染,这也是尽早拥有它的另一个好理由。)

Also, on all forms accepting user input put an accept-charset="utf-8"attribute. Generally browsers submitting POST data will default to the encoding of the page, but it's no harm to be sure.

此外,在所有接受用户输入的表单上放置一个accept-charset="utf-8"属性。通常浏览器提交 POST 数据会默认为页面的编码,但可以肯定的是没有坏处。

回答by agsamek

I used the mentioned methods and they worked fine. Until recently, when my provider has updated PHP to 5.2.11 and MySQL to 5.0.81-community. After this change the unicode characters were properly retrieved from the database, but all updates were corrupted and unicode characters were being replaced by '?'.

我使用了上面提到的方法,它们工作得很好。直到最近,当我的提供商将 PHP 更新到 5.2.11 并将 MySQL 更新到 5.0.81-community 时。在此更改之后,Unicode 字符已从数据库中正确检索,但所有更新均已损坏,并且 Unicode 字符被替换为“?”。

The solution was to use:

解决方案是使用:

mysql_set_charset('utf8',$conn);

It was required even though we used:

即使我们使用了它,它也是必需的:

SET NAMES utf8
SET CHARACTER SET utf8

Also - since we have used ADOdb then we needed to find the PHP connection handle. We used the following statement:

另外 - 由于我们使用了 ADOdb,所以我们需要找到 PHP 连接句柄。我们使用了以下语句:

mysql_set_charset('utf8',$adoConn->_connectionID);