How to set UTF-8 encoding in Zend app?

I am developing a Zend application. The data in my database is encoded in "utf8_unicode_ci". I stated in my application.ini application:

resources.view.encoding = "UTF-8" 

but whenever I try to get a string containing special characters like

{'รฉ', 'ร ', 'รจ', ...} in db, the line does not appear if I do not use this function: utf8_decode()

So, I tried to set the encoding in UTF-8 to:

Loading tray:

 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 characters on the page, but when I retrieve items from the database, special characters are not displayed.

  • escaped string returns null ( $this->escape($string) )
  • echo $string replace special characters with ?

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

thanks for your help!

+6
source share
9 answers

Perhaps you should try editing the source files, saving UTF-8 as the encoding of the editor. Sometimes even if you use UTF-8 in your HTML headers, database encoding, etc. If the source files have a different encoding, this can lead to such errors.

+1
source

Have you installed the following to get data from MySQL as utf8?

 resources.db.params.charset = "utf8" 

To display the correct characters correctly, three things must be done:

  • Save utf8 encoded PHP / HTML files
  • Get data from MySQL as utf8
  • Send the correct content / encoding header or use a meta tag

More information in an article by Rob Allen .

+16
source

I had the same problem when using the Doctrine2 module in a Zend Framework 2 application. Explicit character set setting for my Doctrine2 module solved the problem ...

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

 'params' => array( 'host' => 'localhost', 'port' => 'yourport', 'user' => 'username', 'password' => 'password', 'dbname' => 'yourdatabase', 'charset' => 'utf8', ) 

It is also possible to work for a normal database connection. Add the line '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\'' ) ), 
+7
source

Have you also tried setting headers in utf8? Usually in php I do it like this

  header ('Content-type: text/html; charset=utf-8'); 

in your case, I think you should use something else. I took this example from the Zend Framework Documentation , maybe you should use something else, I am not a Zend_Framework expert

 // 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 that you only set meta and encoding)

(look at this question to understand what I mean, answer from Berry Langerak: PHP Special Display Characters )

EDIT - I also found another example in this article where it sets the header for the controller, take a look at it, maybe this is what you are looking for: http://www.chris.lu/en/news/show/4d56d0ecb058c/

This part may 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; } 
+3
source

If you need to use utf8_encode (), this means that the data in your database is not encoded in UTF8. When you say that your database is encoded in "utf8_unicode_ci", this does not really mean that your data is encoded in UTF8.

To make sure the set encoding works with the Zend Framework, using any of the code you show is pretty simple. Using firefox, simply right-click on the page and click on "View page information" if it says "Encoding: UTF8", this means that your page is correctly encoded, but your data is not.

If you used utf8_decode (), this would mean that Zend would not be able to set the encoding.

0
source

If you put all the encoding and matching, for example, "utf8_general_ci" in the BD, and then in application.ini just next to the mysql connection parameters, resources.db.params.charset = "utf8" should do the trick.

0
source

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

 'db' => [ 'adapters' => [ 'dummy' => [], 'myadapter' => ['charset' => 'utf-8',], ], 
0
source

as others said, we need to set the encoding, in my Zend environment I had to do it a little differently:

  1. in the 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 the bootstrap.php file, provide a link to this new line:

'encodings' => $ dbConfig-> encodings

 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 in UTF-8, here is how I did it: 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; 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; 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
source

I used to develop a ZF application with many languages, including RTL. and I used to add this to my boot file, and I found it pretty neat.

This may not be the answer, but I think its a good suggestion :

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

for more information on mb_internal_string check: http://php.net/manual/en/function.mb-internal-encoding.php

-2
source

Source: https://habr.com/ru/post/890038/


All Articles