Arabic languages in Codeigniter

Writing an application is easy. Writing an application that supports all characters from multiple languages? Not so easy.

The main problem comes from way back, when the main language in computing was English. The ASCII characters were given numbers from 1 to 128 a-z, A-Z, 0-9 and punctuation. That is fine for the English language but pretty much every other language out there has characters that don’t fit in there. To address this, we have UTF-8, which can store extra characters as multiple-bits and is backwards compatible with ASCII.

To make your CodeIgniter application play nicely with UTF-8 you have a few things to think about.

Set the HTTP header in index.php

All requests to CodeIgniter are made through the index.php file which by default sits outside the system/ folder. For this reason it makes a perfect place to add a PHP header for me.

header('Content-Type: text/html; charset=utf-8');

Tell CodeIgniter what’s going on

CodeIgniter by default is set to use UTF-8 for much of its internal functionality, so just make sure the charset is set to UTF-8 in your application/config/config.php file.

$config['charset'] = "UTF-8";

Configure database connection

$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_unicode_ci";

The default here is normally utf8_general_ci which is a pretty weak for of Unicode. This is a quote from StackOverflowwhich I think explains things pretty well.

“utf8_unicode_ci is generally more accurate for all scripts. For example, on Cyrillic block: utf8_unicode_ci is fine for all these languages: Russian, Bulgarian, Belarusian, Macedonian, Serbian, and Ukrainian. While utf8_general_ci is fine only for Russian and Bulgarian subset of Cyrillic. Extra letters used in Belarusian, Macedonian, Serbian, and Ukrainian are sorted not well.”

Set up or convert your MySQL

To get this working you need to use MySQL 4.1 (or higher) with utf8 support enabled, but this is pretty standard for most web-hosts.

COLLATE utf8_unicode_ci
DEFAULT COLLATE utf8_unicode_ci;

If you already have the database set up and running, you can use the following code to convert the database to use UTF-8.

COLLATE utf8_unicode_ci
DEFAULT COLLATE utf8_unicode_ci;

Now the database is ready, you need to add in some tables.

title VARCHAR(100) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
body TEXT COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
) DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 
As with the database, if your tables already exist then you can use the following code to convert your data. You might get some fairly strange results if it is full of data in another non-English character set, but on the whole this has never been an issue for me. ALTER TABLE blog (
title VARCHAR(100) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
body TEXT COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
) DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

And finally, set up your Meta Data.

he last place to set for UTF-8 support is in your HTML in the <head> tag. You can use CodeIgniter’s meta() function in the HTML helper or plain HTML. <html>
<?php echo meta('Content-type', 'text/html; charset='.config_item('charset'), 'equiv');?>
<!-- or -->
<meta http-equiv="content-type" content="text/html; charset=<?php echo config_item('charset');?>" />

I have used the config item instead of just putting in UTF-8 as it makes more sense from a programming point of view. If for any reason your charset is changed in the future, that is one less place to change it.

Now your CodeIgniter application is ready, you need to make sure your database GUI is too. I regularly use Navicat and phpMyAdmin and on both you can set the “MySQL connection collation”, so make sure that is set to “utf8_unicode_ci” too or it could show characters wrong and will most likely corrupt your data as you work on it.

For more help developing with UTF-8, take a look at “Handling UTF-8 with PHP” which will explain some of the problems of using the normal string functions on UTF-8 strings, then take a look at PHP’s “Multibyte String Functions” manual pages to learn how to handle your new happily stored UTF-8 data.


Helper dropdown country code ( Codeigniter )

Country codes are defined by the International Standard ISO 3166-1-alpha-2. When writing an application that has to recognise and handle different countries, it’s handy to have a drop-down helper for users to nominate a country.

There are two useful components on this page: o – the country code array (which you can easily use for your own applications) and o – a helper to generates the HTML for a drop-down box, and takes a few parameters.

Watermark Studios originally published a country code array in thread 141781 on 2010-01-17:

$config['country_list'] = array(
"AG"=>"Antigua and Barbuda",
"BA"=>"Bosnia and Herzegovina",
"IO"=>"British Indian Ocean",
"BF"=>"Burkina Faso",
"CV"=>"Cape Verde",
"KY"=>"Cayman Islands",
"CF"=>"Central African Republic",
"CX"=>"Christmas Island",
"CC"=>"Cocos (Keeling) Islands",
"CD"=>"Congo, Democratic Republic of the",
"CG"=>"Congo, Republic of the",
"CK"=>"Cook Islands",
"CR"=>"Costa Rica",
"CZ"=>"Czech Republic",
"DO"=>"Dominican Republic",
"TL"=>"East Timor",
"SV"=>"El Salvador",
"GQ"=>"Equatorial Guinea",
"FK"=>"Falkland Islands (Malvinas)",
"FO"=>"Faroe Islands",
"GF"=>"French Guiana",
"PF"=>"French Polynesia",
"HK"=>"Hong Kong",
"CI"=>"Ivory Coast (C&ocirc;te dd37bbbb63a10ed4a19bdc7b5b9efbcd25f4789f1#39;Ivoire)",
"KR"=>"Korea, South",
"MK"=>"Macedonia, Republic of",
"MH"=>"Marshall Islands",
"AN"=>"Netherlands Antilles",
"NC"=>"New Caledonia",
"NZ"=>"New Zealand",
"NF"=>"Norfolk Island",
"PS"=>"Palestinian Territory",
"PG"=>"Papua New Guinea",
"PN"=>"Pitcairn Island",
"SH"=>"Saint Helena",
"KN"=>"Saint Kitts and Nevis",
"LC"=>"Saint Lucia",
"PM"=>"Saint Pierre and Miquelon",
"VC"=>"Saint Vincent and the Grenadines",
"SM"=>"San Marino",
"ST"=>"S&atilde;o Tome and Principe",
"SA"=>"Saudi Arabia",
"CS"=>"Serbia and Montenegro",
"SL"=>"Sierra Leon",
"SB"=>"Solomon Islands",
"ZA"=>"South Africa",
"GS"=>"South Georgia and the South Sandwich Islands",
"LK"=>"Sri Lanka",
"SJ"=>"Svalbard and Jan Mayen",
"TT"=>"Trinidad and Tobago",
"TC"=>"Turks and Caicos Islands",
"AE"=>"United Arab Emirates",
"GB"=>"United Kingdom",
"US"=>"United States",
"UM"=>"United States Minor Outlying Islands",
"VA"=>"Vatican City",
"VG"=>"Virgin Islands, British",
"WF"=>"Wallis and Futuna",
"EH"=>"Western Sahara",

Watermark Studios published, in thread 141730, the following code that can be used within a helper.

The helper function takes four parameters: 1) Select name – the , defaults to ‘country’  2) Top countries to show in list (array)  3) Selected country (eg. ‘Australia’)  4) Show all countries (TRUE = show all, FALSE = only show top countries)

Example usage:

echo country_dropdown('country', array('US','CA','GB','DE','BR','IT','ES','AU','NZ','HK'));
// Returns a list of ALL countries, with the listed 10 shown at the top of the list

And the helper code is thus:

function  country_dropdown ( $name="country", $top_countries=array(), $selection=NULL, $show_all=TRUE )  {
    // You may want to pull this from an array within the helper
    $countries = config_item('country_list');

    $html = "<select name='{$name}'>";
    $selected = NULL;
    if(in_array($selection,$top_countries))  {
        $top_selection = $selection;
        $all_selection = NULL;
    else  {
        $top_selection = NULL;
        $all_selection = $selection;

    if(!empty($top_countries))  {
		$html .= "<optgroup label='Top Countries'>";
        foreach($top_countries as $value)  {
            if(array_key_exists($value, $countries))  {
                if($value === $top_selection)  {
                    $selected = "SELECTED";
                $html .= "<option value='{$value}' {$selected}>{$countries[$value]}</option>";
                $selected = NULL;
        $html .= "</optgroup>";

    if($show_all)  {
		$html .= "<optgroup label='All Countries'>";
        foreach($countries as $key => $country)  {
            if($key === $all_selection)  {
                $selected = "SELECTED";
            $html .= "<option value='{$key}' {$selected}>{$country}</option>";
            $selected = NULL;
		$html .= "</optgroup>";

    $html .= "</select>";
    return $html;