CodeIgniter 配置字符集和 UTF-8 支持 [英] CodeIgniter config charset and UTF-8 support

查看:20
本文介绍了CodeIgniter 配置字符集和 UTF-8 支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个在数据库中使用/存储重音字符的网站.我设置了页面模板,以便 config.php 字符集变量与设置匹配,例如:

I'm working on a website which uses / stores accented characters in the database. I have the page template set so that the config.php charset variable matches the setting, e.g.:

<meta charset="<?php echo $this->config->item('charset');?>">

我遇到的问题是,当 $config['charset'] 设置为 UTF-8 时,表单验证失败,就好像没有字符如果包含重音字符,则提交.因此,例如,如果 á 包含在字符串中的任何位置,则必填字段将反弹.字符串减去 á 工作正常.

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'] 更改为 ISO-8859-1 并在插入之前将文本转换为 UTF-8 来使其工作/使用 php 的 utf8_encode()utf8_decode() 从数据库中检索后.这是最好的方法,还是我错过了在 CodeIgniter 中使用带重音字符的 UTF-8 所需的东西?

I've managed to get this working by changing the $config['charset'] to ISO-8859-1 and 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?

感谢任何建议.

推荐答案

您必须确保在任何地方都使用 UTF-8,并且 PHP 和 MySQL 都配置为处理 UTF-8.

You have to make sure that you use UTF-8 everywhere, and that both PHP and MySQL are configure to handle UTF-8.

在html中,添加元标记:

In the html, add the meta-tag:

<meta charset="utf-8" />

并以UTF-8格式保存.这是如何在记事本++中做到这一点.

And save it in UTF-8 format. here is how to do that in notepad++.

定义 MySQL 表以支持 UTF-8,创建表:

Define the MySQL tables to support UTF-8, create table with:

DEFAULT CHARSET=utf8;

并将连接设置为:

mysql_set_charset('utf8', $con);

启用 UTF-8php.ini:

default_charset = "utf-8"

完整的手动检查在 Web 应用程序中前后处理 Unicode

这篇关于CodeIgniter 配置字符集和 UTF-8 支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆