Stop ACTA

CakePHP character encoding / special characters

Posted in CakePHP on 14.02.2009.

This question pops out often, and the solution is very simple.

Many people want to use the so called special characters. I am not sure why are they so special, it seems perfectly reasonable to have other-than-english characters, like umlauts (A1). Being a native non-english speaker, I can understand this perfectly (try getting the letter "Č" with the english character set!). The solution to this problem is UTF-8. Luckily for us, CakePHP 1.2 supports all kinds of crazy stuff with no trouble.

In a few shorts steps:

Source code

Start with your source code. Are your files saved as UTF-8? If not, they should be. This will enable you to use "special characters" in your code (flash messages, etc..)

Database tables

Make sure all your tables and fields have encoding set to utf8.

Database connection(s)

CakePHP has a very nice option for setting the database encoding automagically, just use the following line in your database config:

class DATABASE_CONFIG
{
    var $default = array
        (
            'driver' => 'mysql',
            'connect' => 'mysql_connect',
            'host' => 'localhost',
            'login' => 'root',
            'password' => '',
            'database' => 'neutrinocms_beta',
            'prefix' => '',
            'encoding' => 'utf8' // <-- easy!
        );
}

Application config

Make sure your application is indeed serving pages as UTF-8. In your ~/app/config/core.php use this:

Configure::write('App.encoding', 'UTF-8');

Also check you have this in your layout (in the <head> section):

<?php echo $html->charset(); ?>

That should do it. Your application should now accept any "foreign" characters you input, or write in your code. Is it not lovely to work with CakePHP magic?

Happy baking!

Article comments — View · Add


Page 1 of 3
1 · 2 · 3

Derick Ng :: 15.02.2009 02:41:20
There are however many more quirks when it comes to encodings e.g. truncating or counting of characters etc. I find http://www.phpwact.org/php/i18n/utf-8 explaining the handling of UTF-8 strings with PHP pretty well.
lecterror :: 15.02.2009 17:19:52
Of course, there is a lot more to it, but related to plain PHP. I just wanted to cover the steps to make CakePHP work properly with UTF-8. I just hope I didn't forget anything...
Robert :: 18.02.2009 20:33:28
I did all the steps you mention, but I still have a problem.
I have a function inside a component, which returns an array.
The key and the value of a pair has special characters.
In the controller I then set this array to a $variable to render.
And in the view I use this $variable as the options of a select input.
$form->input(model.field, array('options' => $variable)).
But the option that has the special character is not rendered.
Robert :: 18.02.2009 20:36:17
I tried also passing with the HTML code for special characters, but the input select will just simply render the code.
&eacute; or whatever
lecterror :: 19.02.2009 05:28:52
Hi Robert,

Since your function is returning an array, you have to check the source of your data. Do the array values come from the database? Or are they hard-coded in your component?

If they are hard-coded, re-check the file encoding of your component. If they come from the database, check the encoding of your DB connection, database tables/fields, and last but not least, make sure those values are entered correctly in the database.

This is all I can think of right now.. Try using Cake's own debug tools and Firebug if you're not already, that ought to help in search for clues.