如何在Zend应用程序中将charset设置为UTF-8?

时间:2023-01-04 20:01:52

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()

db中的{'é','à','è',...},除非我使用该函数,否则不会显示字符串:utf8_decode()

So I tried to set the charset to UTF-8 in :

所以我试着将charset设置为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 :

application.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))
  • 转义字符串返回null($ this-> escape($ string))
  • echo $string substitutes special chars with ?
  • echo $ string替换特殊字符?

so I still have to use utf8_decode() to display them. Any suggestion ?

所以我仍然需要使用utf8_decode()来显示它们。有什么建议吗?

thanks for your help !!

谢谢你的帮助 !!

8 个解决方案

#1


1  

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,如果源文件采用不同的编码,也可能导致此类错误。

#2


16  

Have you set the following, to fetch data from MySQL as utf8?

您是否设置了以下内容,以便以utf8的形式从MySQL获取数据?

resources.db.params.charset = "utf8"

It is necessary to do three things to get correct characters displaying correctly:

为了正确显示正确的字符,有必要做三件事:

  1. Save PHP/HTML files in utf8 encoding
  2. 以utf8编码保存PHP / HTML文件
  3. Fetch data from MySQL as utf8
  4. 以utf8的形式从MySQL获取数据
  5. Send the right content-type / charset header or use a meta tag
  6. 发送正确的content-type / charset标头或使用元标记

Further information in Rob Allen's article.

Rob Allen的文章中的更多信息。

#3


7  

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\''
    )
),

#4


3  

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 documentation maybe 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;

}

#5


0  

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无法设置编码。

#6


0  

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”即可。

#7


0  

I had the same problem, i fixed it in config/autoload/global.php by setting charset value :

我有同样的问题,我通过设置charset值修复它在config / autoload / global.php中:

'db' => [
    'adapters' => [
        'dummy' => [],
        'myadapter' => ['charset'  => 'utf-8',],
    ],

#8


-2  

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语言。我曾经把它添加到我的bootstrap文件中,我发现它非常整洁。

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_string check out : http://php.net/manual/en/function.mb-internal-encoding.php

有关mb_internal_string的更多信息,请访问:http://php.net/manual/en/function.mb-internal-encoding.php

#1


1  

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,如果源文件采用不同的编码,也可能导致此类错误。

#2


16  

Have you set the following, to fetch data from MySQL as utf8?

您是否设置了以下内容,以便以utf8的形式从MySQL获取数据?

resources.db.params.charset = "utf8"

It is necessary to do three things to get correct characters displaying correctly:

为了正确显示正确的字符,有必要做三件事:

  1. Save PHP/HTML files in utf8 encoding
  2. 以utf8编码保存PHP / HTML文件
  3. Fetch data from MySQL as utf8
  4. 以utf8的形式从MySQL获取数据
  5. Send the right content-type / charset header or use a meta tag
  6. 发送正确的content-type / charset标头或使用元标记

Further information in Rob Allen's article.

Rob Allen的文章中的更多信息。

#3


7  

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\''
    )
),

#4


3  

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 documentation maybe 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;

}

#5


0  

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无法设置编码。

#6


0  

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”即可。

#7


0  

I had the same problem, i fixed it in config/autoload/global.php by setting charset value :

我有同样的问题,我通过设置charset值修复它在config / autoload / global.php中:

'db' => [
    'adapters' => [
        'dummy' => [],
        'myadapter' => ['charset'  => 'utf-8',],
    ],

#8


-2  

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语言。我曾经把它添加到我的bootstrap文件中,我发现它非常整洁。

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_string check out : http://php.net/manual/en/function.mb-internal-encoding.php

有关mb_internal_string的更多信息,请访问:http://php.net/manual/en/function.mb-internal-encoding.php