Ec (2); if it is only a unicode-to-UTF-8 encoding algorithm, it is everywhere on the Internet, but many people copy me from me. I copy you, and I don't understand why and do at all, in addition to providing the simplest php function for converting unicode to UTF-8 encoding, this article also discusses the relationship between these two types of encoding, is serious redundant and expired, because from the popular UTF-8 to the present, has been through the original six-Byte variable encoding to the actual full resident unicode (UCS-2) the stable phase of script ec (2); script
If it's just a unicode-to-UTF-8 encoding algorithm, it's everywhere on the Internet, but many of you copy me. I copy you and don't understand why and do at all, in addition to providing the simplest php function for converting unicode to UTF-8 encoding, this article also discusses the relationship between these two types of encoding, is serious redundant and expired, because from the popular UTF-8 to the present, has been through the original six-Byte variable encoding to the actual full resident unicode (UCS-2) stable stage.
Unicode encoding is the basis for UTF-8 and gb series encoding (gb2312, gbk, and gb18030) conversion. Although we can also directly create a UTF-8-to-these encoding table, but few people will do this, because the variable encoding of UTF-8 is uncertain, so the general use of unicode and gb encoding table, unicode (UCS-2) is actually the basic encoding of UTF-8, UTF-8 is only one of its implementations. There is a corresponding relationship between the two:
Unicode symbol range | UTF-8 encoding method
U00000 0000-u0000 007F | 0 xxxxxxx
U0000 0080-u0000 07FF | 110 xxxxx 10 xxxxxx
U0000 0800-u0000 FFFF | 1110 xxxx 10 xxxxxx 10 xxxxxx
Because currently UTF-8 characters are in the UCS-2, it is unnecessary to consider the situation of 4-6 bytes encoding, similarly, in reverse conversion, if UTF-8 characters of more than four bytes exist, they can be directly considered as garbled characters or converted to the unicode Entity form ("& # long int ), then it is handed over to the browser or related parsing programs for processing. The following is an algorithm for converting unicode into UTF-8 encoding using php:
/*
* The $ c parameter is an int-type value encoded with unicode characters. If data is read in binary format, it is usually converted using hexdec (bin2hex ($ bin_unichar) in php.
*/
Function uni2utf8 ($ c)
{
If ($ c <0x80)
{
$ Utf8char = chr ($ c );
}
Else if ($ c <0x800)
{
$ Utf8char = chr (0xC0 | $ c> 0x06). chr (0x80 | $ c & 0x3F );
}
Else if ($ c <0x10000)
{
$ Utf8char = chr (0xE0 | $ c> 0x0C ). chr (0x80 | $ c> 0x06 & 0x3F ). chr (0x80 | $ c & 0x3F );
}
// Because the UCS-2 only has two bytes, the subsequent situation is not possible, here only describes the unicode HTML Entity encoding usage.
Else
{
$ Utf8char = "& # {$ c };";
}
Return $ utf8char;
}
In the current environment range, we can consider the UTF-8 character set = unicode (UCS-2), but theoretically, the main character set of the Inclusion relationship is as follows:
UTF-8> unicode (UCS-2)> gb18030> gbk> gb2312
Therefore, if the encoding is correct
Gb2312 => gbk => gb18030 => unicode (UCS-2) => UTF-8
Such a transformation process is basically lossless, but instead
UTF-8 => unicode (UCS-2) => gb18030 => gbk => gb2312
Such a transformation process is likely to have unrecognized characters. Therefore, if UTF-8 encoding is used, do not perform reverse conversion and encoding as easily as possible.