TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   UTF8 support (http://www.talkphp.com/advanced-php-programming/4519-utf8-support.html)

ChrisR 06-10-2009 10:01 AM

UTF8 support
 
I want to know how can i make a script have full utf8 support? do i just use utf8_encode()/utf8_decode() ???

tony 06-10-2009 10:35 PM

I don't know much about it (i should look more into it), but a google search took me to this 2 sources. They seem good for an understanding of the uses.

Sakakuchi 06-11-2009 08:56 AM

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 :-)

ChrisR 06-12-2009 03:16 AM

@Sakakuchi

thanx ill give that ago.

zongliang 06-12-2009 09:14 AM

Sakakuchi is right, i just now developed a project with full utf-8.

thanhtung90 12-20-2009 04:02 AM

I did execute commands but every pages still display " ??? ".


All times are GMT. The time now is 08:25 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0