# How you can set charset to UTF-8 inside a Zend application?

I'm developping a Zend application. The information during my database is encoded in "utf8_unicode_ci". I declared during my application.ini :

resources.view.encoding = "UTF-8"


but whenever I attempt to retrieve a String that contains special figures like

within the db, the string does not display unless of course I personally use the function : utf8_decode()

And So I attempted to create 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/htmlcharset=utf8') echo$this->headMeta()

application.ini :

resources.view.encoding = "UTF-8"
resources.db.params.charset = "utf8"


EDIT : Now I'm able to display special chars inside a page, however when I retrieve components from the database, special chars aren't displayed.

• an steered clear of string returns "null" ($this->escape($string))
• echo $string subsitutes special chars with "?" and so i still need to use utf8_decode() to show them. Any suggestion ? interesting help !! Perhaps you have attempted also setting the headers to utf8? Usually in php i actually do it by doing this  header ('Content-type: text/html; charset=utf-8');  inside your situation i believe you have to use different things. i have taken this situation from Zend Framework documentation perhaps you should use different things, i am no expert of Zend_Framework // Within an action controller action: // Set a header$this->getResponse()
->appendBody($content);  Should you set headers, meta and encoding it will work (out of your code it appears in my experience you're only setting meta and encoding) (see this question to know what i'm saying, the solution from Berry Langerak: PHP Display Special Characters) EDIT - i additionally found another example in the following paragraphs where it sets the header for any controller, have a look in internet marketing,perhaps this is exactly what you're searching for : http://www.chris.lu/en/news/show/4d56d0ecb058c/ This part may be what you're searching 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;

}


I did previously develop ZF application with lots of languages including RTL ones . and I did previously add this to my bootstrap file and I have thought it was pretty neat .

maybe this no answer however i think its good suggestion :

public function _initMB(){
mb_internal_encoding("UTF-8");
}


for more information about mb_internal_string take a look at : http://php.net/manual/en/function.mb-internal-encoding.php

Perhaps you have set the next, to fetch data from MySQL as utf8?

resources.db.params.charset = "utf8"


It's important to complete three items to get correct figures exhibiting properly:

1. Save PHP/HTML files in utf8 encoding
2. Fetch data from MySQL as utf8
3. Send the best content-type / charset header or make use of a meta tag