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

前端 未结 9 1408
失恋的感觉
失恋的感觉 2021-01-05 14:29

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 =          


        
相关标签:
9条回答
  • 2021-01-05 14:54

    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.

    0 讨论(0)
  • 2021-01-05 14:56

    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.

    0 讨论(0)
  • 2021-01-05 14:57

    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\''
        )
    ),
    
    0 讨论(0)
  • 2021-01-05 15:01

    as others said we have to set the encoding, in my Zend framework I had to do it in little different way:

    1. in db.ini file:

    charset = "utf8"

    adapter = PDO_MYSQL
    dbname = skydevel_skydevfinal
    username = root
    password = 
    hostname = localhost
    sprofiler = false
    charset = "utf8"
    

    add this line below where you declared the database, username, etc.

    2.Now in bootstrap.php file point the reference to that new line:

    'charset'=>$dbConfig->charset

    end_Db::factory($dbConfig->adapter, array('host' => $dbConfig->host,
                    'username' => $dbConfig->username,
                    'password' => $dbConfig->password,
                    'dbname' => $dbConfig->dbname,
                    'charset'=>$dbConfig->charset
    
    1. you have to set your database and table to UTF-8, here is how I have done: ALTER DATABASE skydevel_skydevfinal CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; ALTER TABLE ch_news_comments CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
    0 讨论(0)
  • 2021-01-05 15:09

    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;
    
    }
    
    0 讨论(0)
  • 2021-01-05 15:11

    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.

    0 讨论(0)
提交回复
热议问题