我正在开发Zend应用程序。我的数据库中的数据用"utf8_unicode_ci“编码。我在我的application.ini中声明:
resources.view.encoding = "UTF-8"
但是每当我试图检索包含特殊字符的字符串时,比如
{'é‘、'à’、'è‘、.}在db中,除非我使用函数:utf8_decode()
,否则字符串不会显示
因此,我尝试将字符集设置为UTF-8:
自举:
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();
application.ini:
resources.view.encoding = "UTF-8"
resources.db.params.charset = "utf8"
编辑:现在我可以在页面中显示特殊字符,但是当我从数据库中检索元素时,就不会显示特殊字符。
null
($this->escape($string)
)echo $string
用?
代替特殊的字符因此,我仍然需要使用utf8_decode()
来显示它们。有什么建议吗?
谢谢你的帮助!!
发布于 2019-06-07 10:02:36
正如其他人所说,我们必须设置编码,在我的Zend框架中,我不得不以一种几乎不同的方式来实现它:
字符集= "utf8"
adapter = PDO_MYSQL
dbname = skydevel_skydevfinal
username = root
password =
hostname = localhost
sprofiler = false
charset = "utf8"
在声明数据库、用户名等的下面添加这一行。
2.现在在bootstrap.php文件中指向对新行的引用:
'charset'=>$dbConfig->charset
end_Db::factory($dbConfig->adapter, array('host' => $dbConfig->host,
'username' => $dbConfig->username,
'password' => $dbConfig->password,
'dbname' => $dbConfig->dbname,
'charset'=>$dbConfig->charset
ALTER DATABASE skydevel_skydevfinal CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; ALTER TABLE ch_news_comments CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
https://stackoverflow.com/questions/6276150
复制相似问题