Thread: UTF8 support
View Single Post
Old 06-11-2009, 08:56 AM   #3 (permalink)
Sakakuchi
The Contributor
 
Sakakuchi's Avatar
 
Join Date: Feb 2009
Posts: 64
Thanks: 1
Sakakuchi is on a distinguished road
Default

To make a fully working Page with UTF8 encoding you just need to follow some simple rules:

1. Make sure your Database Fields are throughout UTF8 encoded.

2. Make the Database Connection to UTF8. Just fire right after you opened the DB-Connection the following query:
PHP Code:
mysql_query("SET NAMES utf8"); 
You could also when building a new Database set the encoding of the connection (But I would recomend to keep the mysql-query since you never know, wheter you'll change the Database once etc.).

3. Next set the encoding in your IDE to UTF8. Under Eclipse you'll find it under 'Edit/Set Encoding' = for 1 File or under 'Project/Preferences/Resource' = whole Project. Every proper Editor should be able to Set File encodings

4. Now that we have everything done on our Server Side - we just need to make sure that the Browser knows that some UTF8 encoded Data is comming - start of with the PHP header:
PHP Code:
header("Content-Type: text/html; charset=utf-8"); 
Now we just add a Meta Tag to our HTML where it also says that this Data is UTF8 encoded, to make double sure that the browser interprets our data as UTF8:

HTML Code:
 <meta http-equiv="content-type" content="text/html; charset=UTF-8" />

utf8_encode() and -decode() - you would only need when you want to have Plaintext e.g. you comunicate with an external site (e.g. SMS-interface etc.) - and need to pass plain text there. Or need to convert the UTF-8 to some other format (Some pervert Example since I cant think of an better): your Database is not UTF-8 encoded - so you take the data comming from there - decode it to plaintext - and encode it again to utf8 - and now can fire it out to the browser
Sakakuchi is offline  
Reply With Quote
The Following 2 Users Say Thank You to Sakakuchi For This Useful Post:
ChrisR (06-19-2009), Runar (06-12-2009)