如何最好地配置 PHP 以处理 UTF-8 网站

2021-12-28 00:00:00 utf-8 php

您会推荐哪些扩展以及如何最好地配置 php 以创建一个对所有内容都使用 utf-8 编码的网站.例如...

What extensions would you recommend and how should php be best configured to create a website that uses utf-8 encoding for everything. eg...

  • 页面输出为 utf-8
  • 表单提交以 utf-8 编码的数据
  • 字符串数据的内部处理(例如与数据库对话时)也都是 utf-8 格式.

目前看来 php 并不能很好地处理多字节字符集.到目前为止,我已经发现 mbstring 看起来像是一个重要的扩展.

It seems that php does not really cope well with multibyte character sets at the moment. So far I have worked out that mbstring looks like an important extension.

值得麻烦吗..?

推荐答案

PHP 与 Unicode 内容的假设问题有些夸大其词.自 1998 年以来,我一直在做多语言网站,直到我在某处读到它时才知道可能会出现问题 - 多年后和网站.

The supposed issues of PHP with Unicode content have been somewhat overstated. I've been doing multilingual websites since 1998 and never knew there might be an issue until I've read about it somewhere - many years and websites later.

这对我来说很好用:

Apache 配置(在 httpd.conf 或 .htaccess 中)

Apache configuration (in httpd.conf or .htaccess)

AddDefaultCharset utf-8

PHP(在 php.ini 中)

PHP (in php.ini)

default_charset = "utf-8"
mbstring.internal_encoding=utf-8
mbstring.http_output=UTF-8
mbstring.encoding_translation=On
mbstring.func_overload=6 

MySQL

CREATE 使用 utf8_* 整理的数据库,让表继承数据库排序规则和使用 "SET NAMES utf8"

CREATE your database with an utf8_* collation, let the tables inherit the database collation and start every connection with "SET NAMES utf8"

HTML(在 HEAD 元素中)

HTML (in HEAD element)

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

相关文章