如何最好地配置 PHP 以处理 UTF-8 网站
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1605760/
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
How to best configure PHP to handle a UTF-8 website
提问by Rik Heywood
What extensions would you recommend and how should php be best configured to create a website that uses utf-8 encoding for everything. eg...
您会推荐哪些扩展以及如何最好地配置 php 以创建一个对所有内容都使用 utf-8 编码的网站。例如...
- Page output is utf-8
- forms submit data encoded in utf-8
- internal processing of string data (eg when talking to a database) are all in utf-8 as well.
- 页面输出为 utf-8
- 表单提交以 utf-8 编码的数据
- 字符串数据的内部处理(例如在与数据库交谈时)也都是 utf-8 格式。
It seems that php does not really cope well with multibyte character sets at the moment. So far I have worked out that mbstringlooks like an important extension.
目前看来 php 并不能很好地处理多字节字符集。到目前为止,我已经发现mbstring看起来像是一个重要的扩展。
Is it worth the hassle..?
值得麻烦吗..?
回答by djn
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.
PHP 与 Unicode 内容的假设问题有点被夸大了。自 1998 年以来,我一直在做多语言网站,直到我在某处读到它之前,我才知道可能会出现问题 - 多年和网站之后。
This works just fine for me:
这对我来说很好用:
Apache configuration (in httpd.conf or .htaccess)
Apache 配置(在 httpd.conf 或 .htaccess 中)
AddDefaultCharset utf-8
PHP (in php.ini)
PHP(在 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
MySQL
CREATEyour database with an utf8_*collation,
let the tables inherit the database collation and
start every connection with "SET NAMES utf8"
CREATE您的数据库具有utf8_*排序规则,让表继承数据库排序规则并开始每个连接"SET NAMES utf8"
HTML (in HEAD element)
HTML(在 HEAD 元素中)
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
回答by Neeraj Singh
I was facing same issue for UTF-8 characters, Everything was working on live server and staging server, but sometime it's breaking on my dev machine. The behavior was so strange, some times characters was encoded properly but on random page reload it was start breaking with Diamond Charters'??????????????!???'or Question mark'??????????????!???'or 85% data was rendering properly '???????????!???'but rest 15% was showing unmatched characters. I was looking to fix the issue. So, started with my checklist
我面临同样的问题UTF-8 characters,一切都在实时服务器和临时服务器上工作,但有时它在我的开发机器上坏了。行为真是太奇怪了,有时字符进行编码正确,但随机页面重载它开始打破Diamond Charters'??????????????!???'或Question mark'??????????????!???'或85个%的数据是正确的渲染'???????????!???',但休息15%的出无与伦比的字符。我想解决这个问题。所以,从我的清单开始
1 - Check if Character Header Added in HTML
1 - 检查是否在 HTML 中添加了字符标题
2 - Check if data proper saved in MySQL table
2 - 检查数据是否正确保存在 MySQL 表中
3 - Check if MySQL has proper encoding settings for UTF-8
3 - 检查 MySQL 是否有正确的 UTF-8 编码设置
4 - Check if Apache has Setting to deal with UTF-8 Character set
4 - 检查 Apache 是否具有处理 UTF-8 字符集的设置
5 - Check if simple PHP can echo "???????????" output same as input "???????????"
5 - 检查简单的 PHP 是否可以回显“?????????????” 输出与输入相同“?????????????”
6 - Check if PHP sending proper Headers output
6 - 检查 PHP 是否发送正确的 Headers 输出
7 - Check if MySQL Query getting same data "???????????"
7 - 检查 MySQL Query 是否获得相同的数据“???????????”
8 - Check if "???????????" has some html characters, deal with them properly
8 - 检查是否“???????????” 有一些 html 字符,正确处理它们
9 - Check if "???????????" passing through any html encode decode function
9 - 检查是否“???????????” 通过任何 html 编码解码功能
10- Check if .htaccess all set to deal with UTF-8 Character set
10- 检查 .htaccess 是否全部设置为处理 UTF-8 字符集
Check all the above list to figure out where something..breaking.
检查以上所有列表以找出某些东西......破坏的地方。
Give a try (I am using Codeigniter):
试一试(我正在使用 Codeigniter):
=================================
:: PHP ini Settings::
=================================
default_charset = "utf-8"
mbstring.internal_encoding=utf-8
mbstring.http_output=UTF-8
mbstring.encoding_translation=On
mbstring.func_overload=6
=================================
:: .htaccess Settings::
=================================
DefaultLanguage en-US
AddDefaultCharset UTF-8
=================================
:: HTML Header Page::
=================================
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
=================================
:: PHP Codeigniter index.php ::
=================================
header('Content-Type: text/html; charset=UTF-8');
=================================
:: Codeigniter config.php ::
=================================
$config['charset'] = 'UTF-8';
=================================
:: Codeigniter database.php ::
=================================
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
=================================
:: Codeigniter helper function (optional)
=================================
if(!function_exists('safe_utf_string')){
function safe_utf_string($utf8string= ''){
$utf8string = htmlspecialchars($utf8string, ENT_QUOTES, 'UTF-8');
return mb_convert_encoding($utf8string, 'UTF-8');
}
}
and Finally don't forget to say Thanks! :) to @djnanswer
最后别忘了说声谢谢!:) 给@djn回答
回答by MarcoZen
2018 Update :::
2018 更新 :::
Kindly note that these php.ini entries are DEPRECATED;
请注意,这些 php.ini 条目已弃用;
;mbstring.internal_encoding = utf-8
;mbstring.http_input =
;mbstring.http_output = utf-8
Next ...
下一个 ...
PHP - Set utf8 for the following - via a config.php file for your web app
PHP - 为以下设置 utf8 - 通过您的 Web 应用程序的 config.php 文件
ini_set('default_charset', 'UTF-8');
mb_internal_encoding('UTF-8');
iconv_set_encoding('internal_encoding', 'UTF-8');
iconv_set_encoding('output_encoding', 'UTF-8');
MariaDB / MySQL - Set utf8 via:
MariaDB / MySQL - 通过以下方式设置 utf8:
mysqli::set_charset ( "utf8mb4" );
HTML Pages - Set via:
HTML 页面 - 通过以下方式设置:
<meta charset="utf-8" >
回答by James Anderson
php copes just fine!
php应付得很好!
You should set the php.ini "default_charset" parameter to 'utf-8'.
您应该将 php.ini "default_charset" 参数设置为 'utf-8'。
The make sure that:-
确保:-
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=utf-8"
/>
is at the top of every page you serve.
位于您服务的每个页面的顶部。
There are a few problem areas:
有几个问题区域:
Databases -- make sure they are configured to use utf-8 by default or enter a world of pain.
数据库——确保它们被配置为默认使用 utf-8 或进入一个痛苦的世界。
IDEs/Editors -- a lot of editors don't support utf-8 well. I normally use vim which doesn't but its never been a big problem.
IDE/编辑器——很多编辑器不支持 utf-8。我通常使用 vim,它没有,但它从来都不是一个大问题。
Documents -- just spent a whole afternoon getting php to read Thai characters out of a spreadsheet. I was eventually successful but am still not sure what I did right.
文档——刚刚花了一个下午的时间让 php 从电子表格中读取泰语字符。我最终成功了,但仍然不确定我做对了什么。
回答by RSeidelsohn
If mbstring isn't already part of your PHP package, then I definitely would recommend it to you - you'll even want to use it for calculationg string lengths ( mb_strlen($string_var, 'utf8') ) for form input... Else you won't need anything except valid and proper HTML, a correct http-server-config (so the server will deliver pages unsing utf-8) and a text editor with utf-8-support (e.g. Notepad++).
如果 mbstring 还不是你的 PHP 包的一部分,那么我肯定会向你推荐它 - 你甚至想用它来计算字符串长度( mb_strlen($string_var, 'utf8') )以进行表单输入......否则,除了有效且正确的 HTML、正确的 http-server-config(因此服务器将提供不使用 utf-8 的页面)和支持 utf-8 的文本编辑器(例如 Notepad++)之外,您将不需要任何东西。
回答by Ben James
In your php.ini, set
在你的 php.ini 中,设置
mbstring.internal_encoding = UTF-8
mbstring.encoding_translation = On
so that you don't need to pass an encoding parameter to the mb_ functions every time.
这样您就不需要每次都将编码参数传递给 mb_ 函数。

