encoding conversion between gb2312 and Unicode
The following example converts a gb2312 into a "full" form
php4.3.1 after the Iconv function is very useful, just need to write a uft8 to Unicode conversion function
Tabular (gb2312.txt) is OK
Copy CodeThe code is as follows:
$text = "Home of the script";
Preg_match_all ("/[\x80-\xff"?). /", $text, $ar);
foreach ($ar [0] as $v)
echo "Utf8_unicode" (Iconv ("GB2312", "UTF-8", $v)). ";";
?>
UTF8, Unicode
function Utf8_unicode ($c) {
Switch (strlen ($c)) {
Case 1:
Return ord ($c);
Case 2:
$n = (ord ($c [0]) & 0x3f) << 6;
$n + = Ord ($c [1]) & 0x3f;
return $n;
Case 3:
$n = (ord ($c [0]) & 0x1f) << 12;
$n + = (ord ($c [1]) & 0x3f) << 6;
$n + = Ord ($c [2]) & 0x3f;
return $n;
Case 4:
$n = (ord ($c [0]) & 0x0f) << 18;
$n + = (ord ($c [1]) & 0x3f) << 12;
$n + = (ord ($c [2]) & 0x3f) << 6;
$n + = Ord ($c [3]) & 0x3f;
return $n;
}
}
?>
The following example uses PHP to convert the "all" encoding to gb2312.
Copy CodeThe code is as follows:
$str = "TTL 24x7 auto-focus";
$str = Preg_replace ("|&# ([0-9]{1,5}); |", "\". U2UTF82GB (\\1). \ "", $str);
$str = "\ $str =\" $str \ ";";
eval ($STR);
Echo $str;
function U2UTF82GB ($c) {
$str = "";
if ($c < 0x80) {
$str. = $c;
} else if ($c < 0x800) {
$str. =CHR (0xC0 | $c >>6);
$str. =CHR (0x80 | $c & 0x3F);
} else if ($c < 0x10000) {
$str. =CHR (0xE0 | $c >>12);
$str. =CHR (0x80 | $c >>6 & 0x3F);
$str. =CHR (0x80 | $c & 0x3F);
} else if ($c < 0x200000) {
$str. =CHR (0xF0 | $c >>18);
$str. =CHR (0x80 | $c >>12 & 0x3F);
$str. =CHR (0x80 | $c >>6 & 0x3F);
$str. =CHR (0x80 | $c & 0x3F);
}
Return Iconv (' UTF-8 ', ' GB2312 ', $str);
}
?>
or a
Copy CodeThe code is as follows:
function Unescape ($STR) {
$str = Rawurldecode ($STR);
Preg_match_all ("/(?:%u.{4}) |& #x. {4};|&#\d+;|.+/u", $str, $r);
$ar = $r [0];
Print_r ($ar);
foreach ($ar as $k = = $v) {
if (substr ($v, 0,2) = = "%u")
$ar [$k] = Iconv ("UCS-2", "GB2312", Pack ("H4", substr ($v,-4)));
ElseIf (substr ($v, 0,3) = = "& #x")
$ar [$k] = Iconv ("UCS-2", "GB2312", Pack ("H4", substr ($v, 3,-1)));
ElseIf (substr ($v, 0,2) = = "the") {
Echo substr ($v, 2,-1). "
";
$ar [$k] = Iconv ("UCS-2", "GB2312", Pack ("n", substr ($v, 2,-1)));
}
}
return join ("", $ar);
}
$str = "TTL 24x7 auto-focus";
echo unescape ($STR); Out TTL 24x7 Auto focus
Using JavaScript to transform
Copy CodeThe code is as follows:
<title>Unicode</title>
Here is an example of a view showing all full-width half-width fonts
Copy CodeThe code is as follows:
Custom: -
Here is a look-up table (gb2312), conversion gb2312 to UTF8 example, now has the ICONV function, this has not much significance,
Copy CodeThe code is as follows:
function Gb2utf8 ($GB) {
if (!trim ($GB)) return $GB;
$filename = "Gb2312.txt";
$tmp =file ($filename);
$codetable =array ();
while (list ($key, $value) =each ($tmp))
$codetable [Hexdec (substr ($value, 0,6))]=substr ($value, 7,6);
$utf 8 = "";
while ($GB) {
if (Ord (substr ($GB, 0,1)) >127) {
$this =substr ($GB, 0,2);
$GB =substr ($GB, 2,strlen ($GB)-2);
$utf 8.=u2utf8 (Hexdec ($codetable [Hexdec (Bin2Hex ($this)) -0x8080]);
}else{
$this =substr ($GB, 0, 1);
$GB =substr ($GB, 1,strlen ($GB)-1);
$utf 8.=u2utf8 ($this);
}
}
return $UTF 8;
}
function U2utf8 ($c) {
$str = "";
if ($c < 0x80) {
$str. = $c;
} else if ($c < 0x800) {
$str. =CHR (0xC0 | $c >>6);
$str. =CHR (0x80 | $c & 0x3F);
} else if ($c < 0x10000) {
$str. =CHR (0xE0 | $c >>12);
$str. =CHR (0x80 | $c >>6 & 0x3F);
$str. =CHR (0x80 | $c & 0x3F);
} else if ($c < 0x200000) {
$str. =CHR (0xF0 | $c >>18);
$str. =CHR (0x80 | $c >>12 & 0x3F);
$str. =CHR (0x80 | $c >>6 & 0x3F);
$str. =CHR (0x80 | $c & 0x3F);
}
return $str;
}
?>
http://www.bkjia.com/PHPjc/313788.html www.bkjia.com true http://www.bkjia.com/PHPjc/313788.html techarticle encoding conversion between GB2312 and Unicode The following example is converting gb2312 to "" In this form php4.3.1 after the Iconv function is very useful, just need to write a uft8 to the Unicode of the turn ...