如何在 Zend 应用程序中将字符集设置为 UTF-8? [英] How to set charset to UTF-8 in a Zend application?
问题描述
我正在开发 Zend 应用程序.我的数据库中的数据以utf8_unicode_ci"编码.我在 application.ini 中声明:
I am developping a Zend application. The data in my database is encoded in "utf8_unicode_ci". I declared in my application.ini :
resources.view.encoding = "UTF-8"
但是每当我尝试检索包含特殊字符的字符串时,例如
but whenever I try to retrieve a String containing special characters like
{'é', 'à', 'è',...} 在数据库中,除非我使用该函数,否则字符串不会显示:utf8_decode()
{'é', 'à', 'è',...} in the db, the string doesn't display unless I use the function : utf8_decode()
所以我尝试在 :
引导程序:
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"
现在我可以在页面中显示特殊字符,但是当我从数据库中检索元素时,不会显示特殊字符.
EDIT : Now I can display special chars in a page, but when I retrieve elements from the database, special chars are not displayed.
- 转义的字符串返回
null
($this->escape($string)
) echo $string
用?
替换特殊字符
- an escaped string returns
null
($this->escape($string)
) echo $string
substitutes special chars with?
所以我仍然必须使用 utf8_decode()
来显示它们.有什么建议吗?
so I still have to use utf8_decode()
to display them. Any suggestion ?
感谢您的帮助!!
推荐答案
也许您应该尝试编辑源文件,保持 UTF-8 作为编辑器的编码.有时,即使您在 HTML 标头、数据库编码等中使用 UTF-8,如果源文件采用不同的编码,也可能导致此类错误.
Maybe you should try to edit your source files keeping UTF-8 as your editor's encoding. Sometimes even if you use UTF-8 in your HTML headers, database encoding, etc, if your source files are in a different encoding, this could lead to that kind of errors.
这篇关于如何在 Zend 应用程序中将字符集设置为 UTF-8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!