Php code conversion program code. Let's take a look at the code of an encoding and conversion program written in php, and convert between gbk and UTF-8. FunctionphpUnescape_no ($ source) {$ decodedStr; $ pos0; $ lenstrlen ($ source); w let's take a look at the code of an encoding conversion program written in php, UTF-8 conversion.
Function phpUnescape_no ($ source ){
$ DecodedStr = "";
$ Pos = 0;
$ Len = strlen ($ source );
While ($ pos <$ len ){
$ CharAt = substr ($ source, $ pos, 1 );
If ($ charAt = '% '){
$ Pos ++;
$ CharAt = substr ($ source, $ pos, 1 );
If ($ charAt = 'u '){
// We got a unicode character
$ Pos ++;
$ UnicodeHexVal = substr ($ source, $ pos, 4 );
$ Unicode = hexdec ($ unicodeHexVal );
$ Entity = "& #". $ unicode .';';
$ DecodedStr. = utf8_encode ($ entity );
$ Pos + = 4;
}
Else {
// We have an escaped ascii character
$ HexVal = substr ($ source, $ pos, 2 );
$ DecodedStr. = chr (hexdec ($ hexVal ));
$ Pos + = 2;
}
} Else {
$ DecodedStr. = $ charAt;
$ Pos ++;
}
}
Return $ decodedStr;
}
//////////////////////////////////////// //////////////////////////////
Function phpUnescape ($ escstr ){
Preg_match_all ("/% u [0-9A-Za-z] {4} | %. {2} | * | [0-9a-zA-Z. +-_] +/", $ escstr, $ matches); // prt ($ matches );
$ Ar = & $ matches [0];
$ C = "";
Foreach ($ ar as $ val ){
If (substr ($ val, 0, 1 )! = "%") {// If it is an ascii code of letters, numbers, + -_.
$ C. = $ val;
}
Elseif (substr ($ val, 1, 1 )! = "U") {// if it is a non-alphanumeric plus-_. ascii code
$ X = hexdec (substr ($ val, 1, 2 ));
$ C. = chr ($ x );
}
Else {// if it is a code greater than 0xFF
$ Val = intval (substr ($ val, 2), 16 );
If ($ val <0x7F) {// else -007f
$ C. = chr ($ val );
} Elseif ($ val <0x800) {// 0080-0800
$ C. = chr (0xC0 | ($ val/64 ));
$ C. = chr (0x80 | ($ val % 64 ));
} Else {// 0800-FFFF
$ C. = chr (0xE0 | ($ val/64)/64 ));
$ C. = chr (0x80 | ($ val/64) % 64 ));
$ C. = chr (0x80 | ($ val % 64 ));
}
}
}
Return UTFtoGBK ($ c );
}
Function UTFtoGBK ($ CS ){
$ CodeObj = new Chinese ("UTF8", "GBK ");
Return $ CodeObj-> Convert ($ CS );
}
Function phpEscape ($ str ){
$ ReturnStr = "";
If (@ function_exists ('MB _ convert_encoding ')){
$ ReturnStr = phpEscape_yes ($ str );
}
Else {
$ ReturnStr = phpEscape_no ($ str );
}
Return $ returnStr;
}
Function phpEscape_yes ($ string, $ encoding = 'gbk '){
$ Return = '';
For ($ x = 0; $ x <mb_strlen ($ string, $ encoding); $ x ++ ){
$ Str = mb_substr ($ string, $ x, 1, $ encoding );
If (strlen ($ str)> 1 ){
$ Return. = '% u'. strtoupper (bin2hex (mb_convert_encoding ($ str, 'ucs-2', $ encoding )));
} Else {
$ Return. = '%'. strtoupper (bin2hex ($ str ));
}
}
Return $ return;
}
Function phpEscape_no ($ str ){
Preg_match_all ("/[x80-xff]. | [x01-x7f] +/", $ str, $ newstr );
$ Ar = $ newstr [0];
Foreach ($ ar as $ k => $ v ){
If (ord ($ ar [$ k]) & gt; = 127 ){
$ TmpString = bin2hex (GBKtoUCS2 ($ v ));
If (! Eregi ("WIN", PHP_ OS )){
$ TmpString = substr ($ tmpString, 2, 2). substr ($ tmpString, 0, 2 );
}
$ ReString. = "% u". $ tmpString;
} Else {
$ ReString. = rawurlencode ($ v );
}
}
Return $ reString;
}
Function GBKtoUCS2 ($ CS ){
$ CodeObj = new Chinese ("GBK", "UTF8 ");
Return utf8ToUnicode ($ CodeObj-> Convert ($ CS ));
}
Function utf8ToUnicode ($ str, $ order = "big ")
{
$ Ucs2string = "";
$ N = strlen ($ str );
For ($ I = 0; $ I <$ n; $ I ++ ){
$ V = $ str [$ I];
$ Ord = ord ($ v );
If ($ ord <= 0x7F) {// 0 xxxxxxx
If ($ order = "little "){
$ Ucs2string. = $ v. chr (0 );
}
Else {
$ Ucs2string. = chr (0). $ v;
}
}
Elseif ($ ord <0xE0 & ord ($ str [$ I + 1])> = 0x80) {// 110 xxxxx 10 xxxxxx
$ A = (ord ($ str [$ I]) & 0x3F) <6;
$ B = ord ($ str [$ I + 1]) & 0x3F;
$ UcsCode = dechex ($ a + $ B); // echot ($ ucsCode );
$ H = intval (substr ($ ucsCode, 0, 2), 16 );
$ L = intval (substr ($ ucsCode, 2, 2), 16 );
If ($ order = "little "){
$ Ucs2string. = chr ($ l). chr ($ h );
}
Else {
$ Ucs2string. = chr ($ h). chr ($ l );
}
$ I ++;
} Elseif ($ ord <0xF0 & ord ($ str [$ I + 1])> = 0x80 & ord ($ str [$ I + 2])> = 0x80) {// 1110 xxxx 10 xxxxxx 10 xxxxxx
$ A = (ord ($ str [$ I]) & 0x1F) <12;
$ B = (ord ($ str [$ I + 1]) & 0x3F) <6;
$ C = ord ($ str [$ I + 2]) & 0x3F;
$ UcsCode = dechex ($ a + $ B + $ c); // echot ($ ucsCode );
$ H = intval (substr ($ ucsCode, 0, 2), 16 );
$ L = intval (substr ($ ucsCode, 2, 2), 16 );
If ($ order = "little "){
$ Ucs2string. = chr ($ l). chr ($ h );
}
Else {
$ Ucs2string. = chr ($ h). chr ($ l );
}
$ I + = 2;
}
}
Return $ ucs2string;
}
//////////////////////////////////////// ////////////////
Function unescapeFuncMake ($ Txt ){
If ($ Txt [2]! = "*") Return $ Txt;
$ ETxt = "";
$ MTxt = "egy + nb @ QwXvCWjKPRxVzDl/h7EOMtSa9f6 * FpNr81i_0kqdG2LBcuZIAJYo34m-sT % 5. UH3SYZ0hzt/y@qDTNECf1BpujiO.X6ks + oIR8GPVg9wbm % xJvKLWrn * F4HAe-QladM27Uc5 _";
$ TTxtnum = substr ($ Txt, 0, 2 );
$ TTxt = substr ($ MTxt, 70). substr ($ MTxt, 70), $ TTxtnum );
For ($ ii = 3; $ ii $ W = ($ II-3) % 10;
$ K = strpos ($ TTxt, $ Txt [$ ii], $ w)-$ w;
$ ETxt. = $ MTxt [$ k];
}
Return phpUnescape ($ ETxt );
}
Function escapeFuncMake ($ Txt ){
If ($ Txt = "" | $ Txt [2] = "*") return $ Txt;
$ MTxt = "egy + nb @ QwXvCWjKPRxVzDl/h7EOMtSa9f6 * FpNr81i_0kqdG2LBcuZIAJYo34m-sT % 5. UH3SYZ0hzt/y@qDTNECf1BpujiO.X6ks + oIR8GPVg9wbm % xJvKLWrn * F4HAe-QladM27Uc5 _";
$ BTxt = phpEscape ($ Txt );
$ TTxt = floor (mt_rand (0, 50 ));
$ ETxt = $ TTxt. ($ TTxt> 9? "*":"**");
For ($ ii = 0; $ ii $ K = strpos ($ MTxt, $ BTxt [$ ii]) + ($ ii % 10 );
$ ETxtstr = substr ($ MTxt, 70). substr ($ MTxt, 70), $ TTxt );
$ ETxt. = $ ETxtstr [$ k];
}
Return $ ETxt;
}
Bytes. Function phpUnescape_no ($ source) {$ decodedStr =; $ pos = 0; $ len = strlen ($ source); w...