These are the most common problems:
- Your editor creating PHP / HTML files in
- The web browser that you view on your site through
- Your PHP web application running on a web server
- MySQL database
- In other places where you read / write data from (memcached, API, RSS feeds, etc.)
And a few things you can try:
Customize Your Editor
Make sure your text editor, IDE, or something else that you are writing PHP code saves your files in UTF-8 format. Your FTP client, scp, SFTP client does not need any special UTF-8 settings.
Make sure web browsers know how to use UTF-8
To make sure that all your user browsers know that you need to read / write all data in the form of UTF-8, you can set this in two places.
Content Type Tag
Make sure the META header of the content type indicates UTF-8 as a character set similar to this:
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
HTTP response headers
Verify that the Content-Type response header also indicates UTF-8 as a character set as follows:
ini_set('default_charset', 'utf-8')
MySQL connection setup
Now you know that all the data received from users in UTF-8 format, we need to configure the client connection between PHP and the MySQL database. Theres a general way to do it by simply executing a MySQL query:
SET NAMES utf8;
... and depending on which client / driver you use, there are helper functions to make this easier:
With built-in mysql functions
mysql_set_charset('utf8', $link);
With MySQLi
$mysqli->set_charset("utf8")
* With PDO_MySQL (when connected) *
$pdo = new PDO( 'mysql:host=hostname;dbname=defaultDbName', 'username', 'password', array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8") );
MySQL database
Currently there are quite a lot, you just need to make sure that MySQL knows how to store data in your tables as UTF-8. You can check their encoding by looking at the Collation value at the SHOW TABLE STATUS output (in phpmyadmin this is shown in the list of tables). If your tables are not already in UTF-8 (probably theyre in latin1), you will need to convert them by running the following command for each table:
ALTER TABLE myTable CHARACTER SET utf8 COLLATE utf8_general_ci;
Last thing to watch out for
Now that all these steps are complete, your application should be free from character set problems. There is one thing to keep in mind, most PHP string functions are not unicode, so for example, if you run strlen () on a multibyte character, itll returns the number of bytes in the input, not the number of characters. You can get around this using the Multibyte String PHP extension, although it is not so common for these byte / character problems to cause problems.
Taken here: http://webmonkeyuk.wordpress.com/2011/04/23/how-to-avoid-character-encoding-problems-in-php/