我正在开发 Zend 应用程序。我数据库中的数据以“utf8_unicode_ci”编码。 我在我的 application.ini 中声明:
resources.view.encoding = "UTF-8"
但每当我尝试检索包含特殊字符的字符串时,如
{'é', 'à', 'è',...} 在数据库中,字符串不会显示,除非我使用以下函数:utf8_decode()
所以我尝试在 :
中将字符集设置为 UTF-8Bootstrap :
protected function _initDoctype() {
$this->bootstrap('view');
$view = $this->getResource('view');
$view->doctype('XHTML_STRICT');
$view->setEncoding('UTF-8');
}
protected function _initFrontControllerOutput() {
$this->bootstrap('FrontController');
$frontController = $this->getResource('FrontController');
$response = new Zend_Controller_Response_Http;
$response->setHeader('Content-Type', 'text/html; charset=UTF-8', true);
$frontController->setResponse($response);
$frontController->setParam('useDefaultControllerAlways', false);
return $frontController;
}
布局:
$this->headMeta()->appendHttpEquiv('Content-Type', 'text/html;charset=utf8');
echo $this->headMeta();
应用程序.ini :
resources.view.encoding = "UTF-8"
resources.db.params.charset = "utf8"
编辑:现在我可以在页面中显示特殊字符,但是当我从数据库中检索元素时,不会显示特殊字符。
- 转义字符串返回
null
($this->escape($string)
) echo $string
将特殊字符替换为?
所以我仍然必须使用 utf8_decode()
来显示它们。有什么建议吗?
感谢您的帮助!!
最佳答案
您是否设置了以下内容,以 utf8 格式从 MySQL 获取数据?
resources.db.params.charset = "utf8"
要使正确的字符正确显示,必须做三件事:
- 以 utf8 编码保存 PHP/HTML 文件
- 从 MySQL 中以 utf8 格式获取数据
- 发送正确的内容类型/字符集 header 或使用元标记
更多信息请参见 Rob Allen's article .
关于database - 如何在 Zend 应用程序中将字符集设置为 UTF-8?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6276150/