如何在Zend应用程序中将字符集设置为UTF-8? [英] How to set charset to UTF-8 in a Zend application?

查看:199
本文介绍了如何在Zend应用程序中将字符集设置为UTF-8?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Zend应用程序。我的数据库中的数据编码在utf8_unicode_ci中。
我在我的application.ini中声明:

  resources.view.encoding =UTF-8




$ b在数据库中的

{'é','à','è',...},除非我使用函数: utf8_decode()



所以我试图将字符集设置为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;
}

布局:

  $ this-> headMeta() - > appendHttpEquiv('Content-Type','text / html; charset = utf8'); 
echo $ this-> headMeta();

application.ini:

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

编辑:现在我可以在页面中显示特殊字符,但是当我从数据库检索元素时,不显示特殊字符。




  • 转义的字符串返回 null $ this-> $ c>)

  • echo $ string 替换特殊字符 / li>


所以我仍然必须使用 utf8_decode()才能显示它们。任何建议?



感谢您的帮助!!

解决方案

您应该尝试编辑您的源文件,保持UTF-8作为编辑器的编码。有时,即使您在HTML标头,数据库编码等中使用UTF-8,如果源文件使用不同的编码,这可能会导致这种错误。


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

解决方案

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.

这篇关于如何在Zend应用程序中将字符集设置为UTF-8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆