Jer*_*ith 6 database zend-framework utf-8 character-encoding
我正在开发Zend应用程序.我的数据库中的数据以"utf8_unicode_ci"编码.我在我的application.ini中声明:
resources.view.encoding = "UTF-8"
Run Code Online (Sandbox Code Playgroud)
但每当我尝试检索包含特殊字符的字符串时
db中的{'é','à','è',...},除非我使用该函数,否则不会显示该字符串: utf8_decode()
所以我试着将charset设置为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;
}
Run Code Online (Sandbox Code Playgroud)
布局:
$this->headMeta()->appendHttpEquiv('Content-Type', 'text/html;charset=utf8');
echo $this->headMeta();
Run Code Online (Sandbox Code Playgroud)
application.ini:
resources.view.encoding = "UTF-8"
resources.db.params.charset = "utf8"
Run Code Online (Sandbox Code Playgroud)
编辑:现在我可以在页面中显示特殊字符,但是当我从数据库中检索元素时,不会显示特殊字符.
null($this->escape($string))echo $string 替换特殊字符 ?所以我仍然需要utf8_decode()用来显示它们.有什么建议吗?
谢谢你的帮助 !!
Dav*_*unt 16
您是否设置了以下内容,以便以utf8的形式从MySQL获取数据?
resources.db.params.charset = "utf8"
Run Code Online (Sandbox Code Playgroud)
为了正确显示正确的字符,有必要做三件事:
Rob Allen的文章中的更多信息.
我在Zend Framework 2应用程序中使用Doctrine2模块时遇到了同样的问题.明确设置我的Doctrine2模块的字符集解决了这个问题......
只需添加'charset' => 'utf8'到您的学说连接参数:
'params' => array(
'host' => 'localhost',
'port' => 'yourport',
'user' => 'username',
'password' => 'password',
'dbname' => 'yourdatabase',
'charset' => 'utf8',
)
Run Code Online (Sandbox Code Playgroud)
可能也适用于您的正常数据库连接.添加'charset=utf8'到连接字符串:
'db' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:host=$localhost;dbname=$yourdatabase;charset=utf8',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
)
),
Run Code Online (Sandbox Code Playgroud)
也许您应该尝试编辑源文件,将 UTF-8 保留为编辑器的编码。有时,即使您在 HTML 标头、数据库编码等中使用 UTF-8,如果您的源文件采用不同的编码,也可能会导致此类错误。