Home > PHP > Talking in tongues – UTF8 with CodeIgniter

Talking in tongues – UTF8 with CodeIgniter

February 16th, 2009 Leave a comment Go to comments

PHP 4 & 5 unfortunately have major problems working with UTF8. Hopefully this will be solved with PHP6 and its fancy pants new rendering interface.

Until then we have to make do and mend.

First setup your database to work with UTF8. I won’t waste time telling you how to do this in MySQL as many people far brighter than I have written more on the subject.

CodeIgniter comes with UTF8 enabled out of the box so there is little to do configuration wise. Unfortunately pasting a test string such as: 検索 (which I stole from this Wikipedia page) into a CI input box will quickly lead to calamity as CI tries to save the string as ASCII.

Simple solution then, save the following helper in your system/application/helpers folder:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Rewrite all outgoing text into UTF8 compatible streams
* Author: Matt Carter <m@ttcarter.com>
* Info: http://hash-bang.net/2009/02/utf8-with-codeigniter
*/
function ob_utf8($string) {
	return utf8_decode($string);
}
ob_start('ob_utf8');
 
foreach ($_POST as $key => $val) // Re-write all incoming text into UTF8 streams
	$_POST[$key] = utf8_encode($val);
?>

Then simply add it to your system/application/autoload.php:

1
$autoload['helper'] = array('utf8');

The helper will automatically convert incoming POST data into MySQL compatible UTF8 and convert outgoing text into HTML UTF8 streams.

Categories: PHP Tags: , , ,
  1. Rocky
    October 22nd, 2009 at 12:37 | #1

    Thanks very much.

  2. November 27th, 2009 at 02:58 | #2

    Fantastic solution. Works great, thank you!

  3. mattonik
    December 21st, 2009 at 12:45 | #3

    Thanks for this great helper!

  4. coolgeek
    December 29th, 2009 at 17:56 | #4

    You’ve got a problem here if a $_POST variable is an array.

    Try this:

    foreach ($_POST as $key => $val) // Re-write all incoming text into UTF8 streams
    if (is_array($val))
    $_POST[$key] = array_map(“utf8_encode”, $val);
    else
    $_POST[$key] = utf8_encode($val);

  5. Serge
    January 16th, 2010 at 12:08 | #5

    Nice. Works perfectly.

  6. January 20th, 2010 at 12:04 | #6

    Gread advice. Thanks very much. Works great!

  7. Francesco
    May 3rd, 2010 at 05:11 | #7

    Really thank you very much! It’s Great!

  8. peciorin
    July 21st, 2010 at 04:50 | #8

    Thanks coolgeek for the array tip : you saved my day.

  9. bonatoc
    July 28th, 2010 at 11:16 | #9

    Many thanks. I just spent hours trying to fix it, and your helper did the trick.

  10. Lars
    December 21st, 2010 at 00:32 | #10

    hey there,

    i added support for objects as well, since you have the possiblity to choose between objects and arrays :)

    foreach ($_POST as $key => $val) // Re-write all incoming text into UTF8 streams
    {
    if (is_array($val)) {
    $_POST[$key] = array_map(“utf8_encode”, $val);
    }
    else if (is_object($val))
    {
    foreach($val as $attr => $value)
    $_POST[$key]->{$attr} = uft8_encode($value);
    }
    else {
    $_POST[$key] = utf8_encode($val);
    }
    }

  11. Rahul Shelar
    March 9th, 2011 at 21:31 | #11

    very nice solution

  12. Heisanevilgenius
    November 14th, 2011 at 19:28 | #12

    Thank you!

  13. Justin
    November 21st, 2011 at 07:29 | #13

    Legend! This, along with the mods suggested by Lars, solved a major issue I’ve been having with CI. Thanks.

  14. toma24
    November 4th, 2012 at 01:35 | #14

    Thank you man. It’s very useful

  1. No trackbacks yet.
*