database 如何在 Zend 应用程序中将字符集设置为 UTF-8?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6276150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set charset to UTF-8 in a Zend application?
提问by Jerec TheSith
I am developping a Zend application. The data in my database is encoded in "utf8_unicode_ci". I declared in my application.ini :
我正在开发 Zend 应用程序。我的数据库中的数据以“utf8_unicode_ci”编码。我在 application.ini 中声明:
resources.view.encoding = "UTF-8"
but whenever I try to retrieve a String containing special characters like
但是每当我尝试检索包含特殊字符的字符串时
{'é', 'à', 'è',...} in the db, the string doesn't display unless I use the function : utf8_decode()
{'é', 'à', 'è',...} 在数据库中,除非我使用该函数,否则字符串不会显示: utf8_decode()
So I tried to set the charset to UTF-8 in :
所以我尝试将字符集设置为 UTF-8:
Bootstrap :
引导程序:
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;
}
Layout :
布局 :
$this->headMeta()->appendHttpEquiv('Content-Type', 'text/html;charset=utf8');
echo $this->headMeta();
application.ini :
应用程序.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.
编辑:现在我可以在页面中显示特殊字符,但是当我从数据库中检索元素时,不会显示特殊字符。
- an escaped string returns
null($this->escape($string)) echo $stringsubstitutes special chars with?
- 转义字符串返回
null($this->escape($string)) echo $string用特殊字符替换?
so I still have to use utf8_decode()to display them. Any suggestion ?
所以我还是要使用utf8_decode()来显示它们。有什么建议吗?
thanks for your help !!
感谢您的帮助 !!
采纳答案by David
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.
也许您应该尝试编辑源文件,将 UTF-8 作为编辑器的编码。有时,即使您在 HTML 标头、数据库编码等中使用 UTF-8,如果源文件采用不同的编码,也可能导致此类错误。
回答by David Snabel-Caunt
Have you set the following, to fetch data from MySQL as utf8?
您是否设置了以下内容以从 MySQL 获取数据为 utf8?
resources.db.params.charset = "utf8"
It is necessary to do three things to get correct characters displaying correctly:
需要做三件事才能正确显示正确的字符:
- Save PHP/HTML files in utf8 encoding
- Fetch data from MySQL as utf8
- Send the right content-type / charset header or use a meta tag
- 以 utf8 编码保存 PHP/HTML 文件
- 从 MySQL 获取数据为 utf8
- 发送正确的内容类型/字符集标头或使用元标记
Further information in Rob Allen's article.
Rob Allen 的文章中的更多信息。
回答by Wilt
I had the same problem while using the Doctrine2 module in my Zend Framework 2 application. Explicitly setting the character set for my Doctrine2 module solved the issue...
我在 Zend Framework 2 应用程序中使用 Doctrine2 模块时遇到了同样的问题。为我的 Doctrine2 模块显式设置字符集解决了这个问题......
Just add 'charset' => 'utf8'to your doctrine connection parameters:
只需添加'charset' => 'utf8'到您的学说连接参数:
'params' => array(
'host' => 'localhost',
'port' => 'yourport',
'user' => 'username',
'password' => 'password',
'dbname' => 'yourdatabase',
'charset' => 'utf8',
)
Might also work for your normal database connection. Add 'charset=utf8'to the connection string:
也可能适用于您的正常数据库连接。添加'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\''
)
),
回答by Nicola Peluchetti
Have you tried also setting the headers to utf8? Usually in php i do it this way
您是否也尝试将标头设置为 utf8?通常在 php 我这样做
header ('Content-type: text/html; charset=utf-8');
in your case i think you must use something different. i've taken this example from Zend Framework documentationmaybe you should use something different, i'm no expert of Zend_Framework
在你的情况下,我认为你必须使用不同的东西。我从Zend Framework 文档中获取了这个示例,也许您应该使用不同的东西,我不是 Zend_Framework 的专家
// Within an action controller action:
// Set a header
$this->getResponse()
->setHeader('Content-Type', 'text/html')
->appendBody($content);
If you set headers, meta and encoding it should work (from your code it seems to me you are only setting meta and encoding)
如果您设置标头、元和编码,它应该可以工作(从您的代码来看,在我看来您只是设置元和编码)
(look at this question to understand what i mean, the answer from Berry Langerak: PHP Display Special Characters)
(看看这个问题来理解我的意思,来自 Berry Langerak 的答案:PHP 显示特殊字符)
EDIT - i also found another example in this article where it sets the header for a controller, take a look at it,maybe this is what you are looking for : http://www.chris.lu/en/news/show/4d56d0ecb058c/
编辑 - 我还在这篇文章中找到了另一个例子,它为控制器设置标题,看看它,也许这就是你要找的:http: //www.chris.lu/en/news/show/ 4d56d0ecb058c/
This part might be what you are looking for:
这部分可能是您正在寻找的:
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;
}
回答by Raj
as others said we have to set the encoding, in my Zend framework I had to do it in little different way:
正如其他人所说,我们必须设置编码,在我的 Zend 框架中,我必须以几乎不同的方式进行设置:
- in db.ini file:
- 在 db.ini 文件中:
charset = "utf8"
字符集 = "utf8"
adapter = PDO_MYSQL
dbname = skydevel_skydevfinal
username = root
password =
hostname = localhost
sprofiler = false
charset = "utf8"
add this line below where you declared the database, username, etc.
在您声明数据库、用户名等的位置下方添加这一行。
2.Now in bootstrap.php file point the reference to that new line:
2.现在在 bootstrap.php 文件中指向该新行的引用:
'charset'=>$dbConfig->charset
'charset'=>$dbConfig->charset
end_Db::factory($dbConfig->adapter, array('host' => $dbConfig->host,
'username' => $dbConfig->username,
'password' => $dbConfig->password,
'dbname' => $dbConfig->dbname,
'charset'=>$dbConfig->charset
- you have to set your database and table to UTF-8, here is how I have done:
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;
- 您必须将数据库和表设置为 UTF-8,这是我的做法:
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;
回答by Víctor Guerrero
If you put all the codification and collation like 'utf8_general_ci' in the BD and then in the application.ini just next of mysql connection parameters, resources.db.params.charset = "utf8"should do the trick.
如果你把所有的编码和整理,比如 'utf8_general_ci' 放在 BD 中,然后在 application.ini 中紧挨着 mysql 连接参数,resources.db.params.charset = "utf8"应该可以解决问题。
回答by MedMahmoud
I had the same problem, i fixed it in config/autoload/global.php by setting charset value :
我遇到了同样的问题,我通过设置字符集值在 config/autoload/global.php 中修复了它:
'db' => [
'adapters' => [
'dummy' => [],
'myadapter' => ['charset' => 'utf-8',],
],
回答by zzarbi
If you need to use utf8_encode() it's meaning that the data in your database is not encoded in UTF8. When you say that your database is encoded in "utf8_unicode_ci" it doesn't actually mean that your data is encoded in UTF8.
如果您需要使用 utf8_encode() 这意味着您的数据库中的数据不是以 UTF8 编码的。当您说您的数据库以“utf8_unicode_ci”编码时,实际上并不意味着您的数据是以 UTF8 编码的。
To verify that set encoding is working with Zend Framework by using any of the code you show, it's quite easy. With firefox just right click on the page and click on "View page info" if it's say "Encoding: UTF8" it means that your page is correctly encoded but that your data is not.
要使用您展示的任何代码来验证集编码是否与 Zend Framework 一起工作,这很容易。使用 firefox 只需右键单击页面并单击“查看页面信息”,如果显示“编码:UTF8”,则表示您的页面已正确编码,但您的数据未正确编码。
If you was using utf8_decode(), then it would mean that Zend is failing to set the encoding.
如果您使用的是 utf8_decode(),则意味着 Zend 无法设置编码。
回答by tawfekov
I used to develop ZF application with many languages including RTL ones . and I used to add this to my bootstrap file and I've found it pretty neat .
我曾经使用多种语言开发 ZF 应用程序,包括 RTL 语言。我曾经将它添加到我的引导程序文件中,我发现它非常整洁。
maybe this not an answer but i think its good suggestion:
也许这不是答案,但我认为这是个好建议:
public function _initMB(){
mb_internal_encoding("UTF-8");
}
for more info about mb_internal_stringcheck out : http://php.net/manual/en/function.mb-internal-encoding.php
有关mb_internal_string退房的更多信息:http: //php.net/manual/en/function.mb-internal-encoding.php

