Converting text with different charset, utf8 and iso/ansi

This one is a common problem that I could not solved months ago or even more than a year ago. I usually give up with mixing charset problem that occur in database. Lucky, in past few days I’m working with Arabic, Dutch and French websites. It give me some knowledge (and problem too) how to deal with them.

Okay … here is a few line of codes that I’m looking for so long and bothering me so much!

if (!mb_check_encoding($$fields[$j],’UTF-8’)) {
  $$fields[$j] = iconv(“ISO-8859-1”, “UTF-8”, $$fields[$j]);
  //$$fields[$j] = mb_convert_encoding($$fields[$j], ‘UTF-8’);
}

Don’t ask me about $$field .. it was coded 10 years ago (with php3), I’m too lazy to change it.

As you can see. I use mb_check_encoding() to check what charset is being use. Also I assume the other mixing charset is defined. In western charset, it is ISO-8859-1. In Arabic, in my case is windows-1256.

At last, then we can use iconv() or mb_convert_encoding() to convert.

I feel like stupid, how can I couldn’t solve this kind of issue before. Just 3 lines of codes make me wait so long to understand it!

Colourful theme by Kempstumblr.