Changing your site to be UTF-8

One thing I that really annoys me is how complicated charsets are on the internet. You need to study a ton to get everything working with "strange" characters. Which if you have anyone using a language that is not English comes up quite often.

Just playing around with a portuguese website I have been working on JPM and getting the charsets all changed from the default to UTF-8

 

First thing you have to do is change the following settings in your php.ini file (mbstring is a needed extension to create a transparent string handling system for UTF-8 strings, meaning you do not need to go encoding and decoding text)

mbstring.func_overload = 7
default_charset = "UTF-8"

You will also have to make sure MySQL (or other database) is using UTF-8 so when you input data to the DB it stays as UTF-8 (do this after the mysql_connect and mysql_select_db)

mysql_query("SET NAMES utf8");
mysql_query("SET CHARACTER SET utf8");
 

Each html page will also need to know that it is encoded with UTF-8. Sending the PHP header command means the recipient gets the information in the header right at the start.

<?php header('Content-Type: text/html; charset=utf-8'); ?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

If all of this stuff is done correctly there should not be any need to use utf8_encode or decode unless you are dealing with data that is coming from an external source.