Problem with Zend Framework and UTF-8 characters (æøå)
Hope here are some with more knowledge about Zend Framework than me, I've been trying to search for the answer but I'm not able to fin anything anywhere.
Problem:
When adding the content of a Zend_Form to the database with the use of Zend_Db the characters æ ø å is replaced by øæå
System
- WampServer 2.0i
- Apache 2.2.11
- MySQL 5.1.36
- PHP 5.3.0
- Zend Framework 1.10.0
Modifications done to make it work (which it does not)
application.ini
resources.db.adapter = "pdo_mysql"
resources.db.params.host = "localhost"
resources.db.params.username = "//Brukernavn//"
resources.db.params.password = "//Passord//"
resources.db.params.dbname = "//Database//"
resources.db.params.charset = "utf8"
resources.db.params.driver_options.1002 = "SET NAMES utf8"
resources.db.isDefaultTableAdapter = true
To all forms I've added
->setAttrib('accept-charset', 'utf-8');
And in Bootstrap.php I've placed the following code:
$view->setEncoding('UTF-8');
$view->doctype('XHTML1_STRICT');
$view->headMeta()->appendHttpEquiv('Content-Type', 'text/html;charset=utf-8');
The database is set to utf8_general_ci
Anyone have a tip on how to get it to work?
Soulution:
It's a bug in PHP 5.3.0 and 5.3.1 ( http://bugs.php.net/bug.php?id=47224 ) so I choose to downgra开发者_JAVA技巧de to 5.2.11, and all worked like a charm.
Thanks to Pekka and Greg K for pointing me in the right direction.
Listening to your description, the data departs from the form as UTF-8. Could it be that your database tables themselves are still latin1
?
Solution that worked for me:
All of the above database things, plus adding the UTF-8 encoding headMeta tag to the layout.
So, layout.phtml looks like this:
<?php echo $this->doctype() ?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php echo $this->headMeta(); ?> <!-- This one does the trick! -->
</head>
<body>
<?php echo $this->layout()->content; ?>
</body>
</html>
And Bootstrap.php like this:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initDoctype()
{
$this->bootstrap('view');
$view = $this->getResource('view');
$view->setEncoding('UTF-8');
$view->doctype('XHTML1_STRICT');
$view->headMeta()->appendHttpEquiv('Content-Type', 'text/html;charset=utf-8');
}
}
Now swedish chars like åäö display correctly, and, although not tried, I think chinese or whatever will as well.
use htmlentities($val) to output values encoded
The problem for me turned out to be that I had a decorator using htmlentities()
which defaults to ISO-8859-1
encoding if not specified, instead of UTF-8
for versions of PHP >= 5.4.
In function _initDatabase() must be add the following code.
$db->getConnection()->exec("SET NAMES 'utf8'");
You can reference this here: http://osdir.com/ml/php.zend.framework.db/2008-04/msg00012.html
i have the same issue in my site, i resolved this issue by adding the charset in application.ini file.
here i add the following codes.
resources.db.params.charset = "utf8"
resources.db.params.driver_options.1002 = "SET NAMES utf8"
resources.db.isDefaultTableAdapter = true
精彩评论