3

I'm migrating a project from a CMS called "Freekore" to CodeIgniter, with my old CMS I didn't have this problem but with CI I can't figure out why I have problems with special characters as ñ,á,é,í etc. I think I've tried everything.
I got this in header

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

also added this

<?php header('Content-Type: text/html; charset=UTF-8'); ?>

on database config

$db['local']['char_set'] = 'utf8';
$db['local']['dbcollat'] = 'utf8_general_ci';

and on config file

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

But I still get this

COMUNICACIÓN Y PRODUCCIÓN EDITORIAL

instead of this

COMUNICACIÓN Y PRODUCCIÓN EDITORIAL

ah and also added this to .htaccess

AddDefaultCharset UTF-8

Edit
Using this I found out that Current character set is latin1 but how? I've checked database and tables and are utf8_general_ci

Edit 2
I did a new database and checked every column collation, now I'm using utf8_unicode_ci on the database, on every table and on ever row, but I still have the same problem Current character set is latin1

Edit 3
I decided to use utf8_decode and seemed to work but I still have problems with uppercases

laviku
  • 531
  • 12
  • 32

6 Answers6

2

After hours searching for something that help me resolve the problem I found an answer so easy in this post so before execute the insertion I used this mysql_query("SET NAMES utf8"); and now the characters are shown as they should. Thank you so much for your help.

Community
  • 1
  • 1
laviku
  • 531
  • 12
  • 32
1

did you try to encode your data in UTF-8 before you put them into your new db using this function: http://www.w3schools.com/php/func_xml_utf8_encode.asp

Alex
  • 478
  • 2
  • 11
  • How can I apply this? Do I have to re-insert all the data into the new database? – laviku Aug 12 '14 at 20:35
  • Well if you keep the same database structure you just do it table by table grabbing all the data from the table and inserting that into your new database. Select * FROM your_original_table Then in PHP you just grab all the data and when you re-insert you do : $query = "INSERT INTO table_name VALUES (".utf8_encode($value1).", ".utf8_encode($value2).", ".utf8_encode($value3).",...) – Alex Aug 12 '14 at 20:55
  • I tested with one record, `TÉCNICO` is saved and shown as `TÉCNICO` :'( – laviku Aug 12 '14 at 21:03
  • 1
    I had this issue on one of the things i was doing at work, i'm really really busy at the moment but i'll have a look at it and i'll post back if i can find something useful. – Alex Aug 12 '14 at 21:13
  • Can you echo the variable which is containing TÉCNICO before you insert into the DB, so we can know if it's a problem related to the DB settings or if it's related to something before plz? – Alex Aug 12 '14 at 22:34
  • `string 'TÉCNICO' (length=8)` on database is `TÉCNICO` I changed the collation to `utf8_unicode_ci` but the problem remains, but only on CodeIgniter – laviku Aug 12 '14 at 22:44
  • Maybe you have to setup code igniter to use UTF8 if it's not already done? This might help ? http://stackoverflow.com/questions/8156900/codeigniter-and-charsets – Alex Aug 12 '14 at 22:53
  • 1
    If you know what kind of characters are a problem you can try to use the str_replace function to replace the bad characters by the good ones, i had to do something like that in my table import files...look : $data = str_replace("'", "'", $data); $data = str_replace("\\", "", $data); $data = str_replace("
    ", "", $data); $data = str_replace("
    ", "", $data); $data = str_replace("
    ", "", $data); $data = str_replace(" ", " ", $data);
    – Alex Aug 12 '14 at 23:05
  • 1
    I've found an easier way, I used `utf8_decode` and it's working, but I still can't understand what is causing this issues. Thank you so much for your help! – laviku Aug 12 '14 at 23:08
  • Well, you found the solution yourself that's already a step forward :) You're very welcome – Alex Aug 12 '14 at 23:08
  • Almost found it, I still have issues when the vocal is uppercase :-o – laviku Aug 12 '14 at 23:12
  • If you want to put everything on lower case you can use this php function: strtolower ( $your_string_data ) – Alex Aug 12 '14 at 23:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/59232/discussion-between-laviku-and-alex). – laviku Aug 12 '14 at 23:18
0

The collation of the database table itself is not UTF-8 unicode. Look here for example in phpmyadmin:

enter image description here

You need to update the collation. If you do not have phpmyadmin, you can use the following command:

ALTER TABLE `test_table` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
XaxD
  • 1,429
  • 14
  • 27
0

i think you need to change the database collection to UTF 8
execute the following queries .

ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_unicode_ci;
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;

hope it will solve your issue .

Azeem Hassni
  • 885
  • 13
  • 28
0

Your problem is likely with utf8_general_ci (MySQL 5.x default) as your collation. Most people are surprised that not all UTF8 is the same. For the most part you have 3 general "flavors"

  • utf8_general_ci
  • utf8_unicode_ci
  • utf8mb4

The difference is in how many bytes are being used for UTF8. utf8mb4 is the complete UTF8 character set, while utf8_general_ci is a small subset that covers most latin character sets, but probably doesn't cover the characters you mentioned. If you switch to utf8_unicode_ci it might cover them. The catch is that the more bytes you use, the slower your database will run in processing that data.

This thread discusses it in more detail. What's the difference between utf8_general_ci and utf8_unicode_ci

Community
  • 1
  • 1
Machavity
  • 30,841
  • 27
  • 92
  • 100
0

Echo utf8_encode($row['$your_field']);

zorro
  • 11