I recently got bogged down by a problem using UTF-8 in Yii. Perhaps, this is a general PHP problem rather than specific to the Yii Framework, but anyway documented for tohers to try.
Corrupted Characters
I was writing a utility to import the Yii message files from all my application and inserting/updating them in a message database. However, somewhere in the pipeline the characters were getting corrupted.
I had started writing this utility using Yii’s CLI interface functions but then read about so many problems running charactersets through MS-DOS that I decided to move the utility into the web application framework, expecting that my character corruption problem would just go away.
Well, it didn’t.
My code was scanning the application directories looking for message files, which, for those not familiar with Yii are in an array format, eg:
return array (
'Change password' => 'Changer mot de passe'.
......)
I import the file by using the include function assigning the output to a variable and then loop through that variable picking up each message occurence.
As soon as I echo’d out the message it seemed corrupted and by the time it got into the database, it was defintely corrupted.
I checked my database and table collation method and they were set to utf8_general_ci as expected.
The solution
Was actually in 2 parts. To output UTF-8 characters from php you need to set the output of php to utf-8. I had read about outputting meta strings to the browser to force the browser into utf-8 like
echo
but this didn’t seem to make any difference. I found that you had to use the PHP header fucntion to actually have any effect
header(‘Content-Type: text/html; charset=utf8′);
By this stage my messages were being echo’d out correctly but the still not updating the database corecctly.
I checked whether the DSN connection string included ”charset=utf8″ and it did … but I decided to try and set the connection charset after the connection had been established, just to try and bingo!
So in Yii the code was:
| PHP | | copy code | | ? |
| 1 | |
| 2 | self::$_db=new CDbConnection($db['connectionString'],$db['username'],$db['password']); |
| 3 | self::$_db->charset="UTF8"; |
| 4 | self::$_db->active=true; |
| 5 |
Blog
Photography
Portfolio
Yii Part 1