php CodeIgniter 配置字符集和 UTF-8 支持
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12158043/
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
CodeIgniter config charset and UTF-8 support
提问by Dan Murfitt
I'm working on a website which uses / stores accented characters in the database. I have the page template set so that the config.phpcharset variable matches the setting, e.g.:
我正在开发一个在数据库中使用/存储重音字符的网站。我设置了页面模板,以便config.php字符集变量与设置匹配,例如:
<meta charset="<?php echo $this->config->item('charset');?>">
The problem I'm having is, when $config['charset']is set to UTF-8, the form validation fails and it's as if no characters were submitted if an accented character was included. So, for example, a required field will bounce back if á is included anywhere in the string. The string minus the á works fine.
我遇到的问题是,当$config['charset']设置为UTF-8 时,表单验证失败,如果包含重音字符,就好像没有提交任何字符。因此,例如,如果 á 包含在字符串中的任何位置,则必填字段将反弹。字符串减去 á 工作正常。
I've managed to get this working by changing the $config['charset']to ISO-8859-1and converting text to UTF-8 before inserting / after retrieving from the database with php's utf8_encode()and utf8_decode(). Is this the best way or am I missing something needed in order to get UTF-8, with accented characters, working in CodeIgniter?
我设法通过改变得到这个工作$config['charset'],以ISO-8859-1和/插入前将文本转换为UTF-8 PHP的从数据库中检索后utf8_encode()和utf8_decode()。这是最好的方法,还是我错过了在 CodeIgniter 中使用带重音字符的 UTF-8 所需的东西?
Any advice appreciated.
任何建议表示赞赏。
回答by Kuf
You have to make sure that you use UTF-8 everywhere, and that both PHP and MySQL are configure to handle UTF-8.
您必须确保在任何地方都使用 UTF-8,并且 PHP 和 MySQL 都配置为处理 UTF-8。
In the html, add the meta-tag:
在 html 中,添加元标记:
<meta charset="utf-8" />
And save it in UTF-8 format. here is how to do that in notepad++.
并以UTF-8格式保存。这是在记事本++中如何做到这一点。
Define the MySQL tables to support UTF-8, create table with:
定义 MySQL 表以支持 UTF-8,创建表:
DEFAULT CHARSET=utf8;
And set the connection to:
并将连接设置为:
mysql_set_charset('utf8', $con);
Enable UTF-8in the php.ini:
启用UTF-8在php.ini:
default_charset = "utf-8"
For a full manual check Handling Unicode Front To Back In A Web App

