|
Posted by Dikkie Dik on 10/12/07 20:49
Steve wrote:
> Hi. I need to store chinese characters and Pinyins in a mysql database
> and then read them from php and using the GD library and
> imagettftext() function to create an image based on a chinese ttf font
> of the chinese characters.
>
> I have tried setting the encoding in the database as UTF-8 and BIG5
> but the characters display (both in MYSQL and when displayed via PHP)
> as question marks.
>
> Can anyone help?
>
First of all, you need to inform all programs what encoding you use. If
you use utf-8, you need to inform MySQL by sending the command:
SET NAMES utf8;
directly after connection to the database (I know the command has a
misleading name. Look it up in the manual).
Also, you need to inform the browser by sending a header like:
Content-Type: text/html;charset=utf-8
You can do that from php by using the header function, but it is
probably easier to set this in php.ini (two settings: content-type and
charset).
Some text may already in the database and you may not be sure in what
encoding it is stored. To find out, use the mysqldump utility. You can
then open a dump of the table that contains the texts with an
encoding-aware editor.
PHP itself really doesn't care much what encoding is used, as it treats
the strings as binary strings anyway. PHP itself does not show anything
on your screen; that is left to the browser. The only thing you should
be aware of in PHP is the fact that the encoding used has an impact on
the length of the characters. In utf-8, characters may have different
lengths. If you need to work with substrings, there are special string
functions to deal with multibyte encodings.
Good luck!
Navigation:
[Reply to this message]
|