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

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


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.


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


Have you tried also setting the headers to utf8? Usually in php i do it this way

 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

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

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/

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;}