How to turn GBK encoding into UTF8 format

Source: Internet
Author: User
Tags format character set modify mysql string
Coded Apache Character Set GB 2312
MySQL Character set GBK
Now to write information from MySQL (GBK) to the XML
The encoding format in XML is UTF-8

So, how to encode gbk into UTF8??

Detailed Introduction:
A while ago, I saw Qiushuiwuhen June's article on Gbk,unicode,big5 's conversion.
But there are a few less big problems

So I designed a class that is responsible for character conversion, fixed some of these deficiencies, added some features, and I will continue to expand the class to support more character sets

Added the following points:

Conversion of UNICODE->GBK symbol parts
Identification of the euro character (€)
Conversion between BIG5,UNICODE,GBK, provided that only the common character set part is converted,

Instructions for use:
Temporary programs support the following character encoding methods:
Gbk,big5,utf-16be (Unicode Big-endian byte order), Utf-16le (Unicode Little-endian byte order), UTF-8
The default input encoding mode is GBK, and the default output encoding is UTF-16BE;
This category provides two functions to modify the input and output encoding methods:
Modify Input Encoding Boolean setgetencoding (String $GetEncoding)
Modify Output Encoding Boolean settoencoding (String $ToEncoding)
function parameters can only be used in the above 5 encoding methods, case-sensitive, such as GBK can not be written GBK
Returns true if the setting succeeds, returns False if the wrong encoding name is used, and the actual error message

Function
String encodestring (String $String)
Responsible for character encoding conversion, returning the converted string

Before using, use the var $FilePath =\ "\" as the absolute path to the program file, or you will not find the data file

Example:
Converts a GBK encoded string to a UTF-8 encoding:

$s =\ "GBK code \";
$CharEncoding =new Encoding ();
$CharEncoding->setgetencoding (\ "Gbk\") | | Die (\ "code name error \");
$CharEncoding->settoencoding (\ "utf-8\") | | Die (\ "code name error \");
Echo $CharEncoding->encodestring ($s);

Using UTF-8 encoding in your browser, you will see the correct characters.

Annex: Encoding.zip



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.