How to set charset to UTF-8 in a Zend application?

2019-02-13 14:17发布

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

{'é', 'à', 'è',...} in the db, the string doesn't display unless I use the function : utf8_decode()

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

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 :

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 $string substitutes special chars with ?

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

thanks for your help !!

8条回答
霸刀☆藐视天下
2楼-- · 2019-02-13 14:57

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.

查看更多
时光不老,我们不散
3楼-- · 2019-02-13 14:59

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

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. Fetch data from MySQL as utf8
  3. Send the right content-type / charset header or use a meta tag

Further information in Rob Allen's article.

查看更多
【Aperson】
4楼-- · 2019-02-13 14:59

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...

Just add 'charset' => 'utf8' to your doctrine connection parameters:

'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:

'db' => array(
    'driver'         => 'Pdo',
    'dsn'            => 'mysql:host=$localhost;dbname=$yourdatabase;charset=utf8',
    'driver_options' => array(
        PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
    )
),
查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-02-13 15:03

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.

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.

If you was using utf8_decode(), then it would mean that Zend is failing to set the encoding.

查看更多
别忘想泡老子
6楼-- · 2019-02-13 15:03

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.

查看更多
我想做一个坏孩纸
7楼-- · 2019-02-13 15:06

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

'db' => [
    'adapters' => [
        'dummy' => [],
        'myadapter' => ['charset'  => 'utf-8',],
    ],
查看更多
登录 后发表回答