Topic: The UTF-8 encoding string into GB2312 encoding, no corresponding encoding string into # DEC; pattern. For example, = #54924; language: PHP, Javascript content: The browser uses the encodeURI function in Javascript to convert strings (including non-GB2312 characters
Topic: convert the UTF-8 encoded string into GB2312 encoding, no corresponding encoding string into & # DEC; pattern. For example, pipeline => pipeline
Language: PHP, Javascript
Content: The browser uses the encodeURI function in Javascript to encode the string (including non-GB2312 characters) and GET requests to the server. the page encoding is GB2312. the PHP script on the server converts the request data to GB2312.
Basics:
1. The iconv function can only be used to convert GB2312 characters. Foreign characters cannot be converted.
2. no ready-made functions can be used
3. bindec () function: converts the '01' string in the binary format to a decimal number.
4. decbin () function: converts a decimal number to a binary string, for example, decbin (224) = '123'
Idea: because the UTF-8 resolution has 1, 2, 3 bytes encoding, Chinese and Japanese Korean are 3 bytes encoding, processing according to the first byte size in the character encoding to distinguish the number of bytes.
1. if the first byte is less than 128, it is an ASCII code.
2.128 ~ 192, non-UTF-8 encoded and processed as & # ord ();
3. 192 ~ 224, dual-byte UTF-8 encoding
4. 224 ~ 240, three-byte encoding
5. 240 ~ 4-byte encoding 248
6 ....
7. try to use iconv to convert the three-byte encoding into GB2312
8. non-GB2312 multi-byte characters, try to convert the UTF-8 to Unicode, and then get the Unicode decimal value
9. you can use bitwise operations or bindec () functions.
Program:
Function GetGB2312String ($ name)
{
$ Tostr = '';
For ($ I = 0; $ I {
$ Curbin = ord (substr ($ name, $ I, 1 ));
If ($ curbin <0x80)
{
$ Tostr. = substr ($ name, $ I, 1 );
} Elseif ($ curbin <bindec ('20140901 ')){
$ Str = substr ($ name, $ I, 1 );
$ Tostr. = '& #'. ord ($ str ).';';
} Elseif ($ curbin <bindec ('20140901 ')){
$ Str = substr ($ name, $ I, 2 );
$ Tostr. = '& #'. GetUnicodeChar ($ str ).';';
$ I = 1;
} Elseif ($ curbin <bindec ('20140901 ')){
$ Str = substr ($ name, $ I, 3 );
$ Gstr = iconv ('utf-8', 'gb2312', $ str );
If (! $ Gstr)
{
$ Tostr. = '& #'. GetUnicodeChar ($ str ).';';
} Else {
$ Tostr. = $ gstr;
}
$ I = 2;
} Elseif ($ curbin <bindec ('20140901 ')){
$ Str = substr ($ name, $ I, 4 );
$ Tostr. = '& #'. GetUnicodeChar ($ str ).';';
$ I = 3;
} Elseif ($ curbin <bindec ('20140901 ')){
$ Str = substr ($ name, $ I, 5 );
$ Tostr. = '& #'. GetUnicodeChar ($ str ).';';
$ I = 4;
} Else {
$ Str = substr ($ name, $ I, 6 );