Use UTF-8 in Zend Framework 1 without the use of utf8_encode() and utf8_decode()

A problem that I was often had trouble with it, is when I use special characters such as French characters like save it in the database and display it again correctly.
There are some important steps to follow so you can show the text without encoding methods such utf8_encode(), iconv(), mb_convert_encoding(), …

Now go start to work in Zend Framework 1:
You get a database “utf8_general_ci” collation.

In your config. Ini file add the following database settings:
; database information
resources.db.adapter = PDO_MYSQL
resources.db.params.host = localhost
resources.db.params.username = root
resources.db.params.password = root
resources.db.params.dbname = mydatabase
resources.db.isDefaultTableAdapter = true
resources.db.params.driver_options.1002 = “SET NAMES UTF8;”

The last line is very important, it ensures that your data is retrieved in UTF-8.

More info: http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html
SET NAMES indicates what character set the client will use to send SQL statements to the server. Thus, SET NAMES ‘cp1251′ tells the server, “future incoming messages from this client are in character set cp1251.” It also specifies the character set that the server should use for sending results back to the client. (For example, it indicates what character set to use for column values if you use a SELECT statement.)

In your header add next following meta tag:
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />

Another little tip, when using Zend_Mail() remember:

$mail = new Zend_Mail(‘UTF-8′);

And now we can work without any problem using UTF-8 characters as in the French language.

Post to Twitter

0 Responses to “Use UTF-8 in Zend Framework 1 without the use of utf8_encode() and utf8_decode()”


  • No Comments

Leave a Reply