CodeIgniter config charset and UTF-8 support

28,197

You have to make sure that you use UTF-8 everywhere, and that both PHP and MySQL are configure to handle UTF-8.

In the html, add the meta-tag:

<meta charset="utf-8" />

And save it in UTF-8 format. here is how to do that in notepad++.

Define the MySQL tables to support UTF-8, create table with:

DEFAULT CHARSET=utf8;

And set the connection to:

mysql_set_charset('utf8', $con);

Enable UTF-8 in the php.ini:

default_charset = "utf-8"

For a full manual check Handling Unicode Front To Back In A Web App

Share:
28,197
Dan Murfitt
Author by

Dan Murfitt

Senior software engineer with 16 years commercial experience. Design, development and maintenance of backend services and web applications for startups, SMEs, public sector, FTSE 100 and Fortune 500 organisations.

Updated on March 24, 2020

Comments

  • Dan Murfitt
    Dan Murfitt about 4 years

    I'm working on a website which uses / stores accented characters in the database. I have the page template set so that the config.php charset variable matches the setting, e.g.:

    <meta charset="<?php echo $this->config->item('charset');?>">
    

    The problem I'm having is, when $config['charset'] is set to UTF-8, the form validation fails and it's as if no characters were submitted if an accented character was included. So, for example, a required field will bounce back if á is included anywhere in the string. The string minus the á works fine.

    I've managed to get this working by changing the $config['charset'] to ISO-8859-1 and converting text to UTF-8 before inserting / after retrieving from the database with php's utf8_encode() and utf8_decode(). Is this the best way or am I missing something needed in order to get UTF-8, with accented characters, working in CodeIgniter?

    Any advice appreciated.

  • Dan Murfitt
    Dan Murfitt over 11 years
    Many thanks for the answer and link - I'll check to make sure UTF-8 is being used throughout. The form validation problem was being caused by the $config['charset'] setting being set to 'UTF-8' (all uppercase) rather than 'utf-8' (all lowercase). For some reason the default setting for this being uppercase 'UTF-8' doesn't work (at least not on my setup).
  • MarcinWolny
    MarcinWolny over 7 years
    Small comment for people who found it on google - alternative source of this problem might be encoding of the table or column (yes, changing encoding of the table does not change the encoding of individual columns) - in that case editing value within a column with UTF-8 string would result in "Incorrect string value” warning, which is not visible when you simply run query through codeigniter. All you'll see is ? instead of UTF-8 char.