_php basis of interchange function of gb2312 and utf-8 without Iconv library

Source: Internet
Author: User
Tags comparison table utf 8

A copy of Gb2312.txt (184799 bytes) does appear to be a bit too large, and is also converted to Unicode.
This table is 51965 bytes, much smaller.
It is also useful for situations where the ICONV function library cannot be used.

<?php
Use of the comparison table
$filename = "Gb2utf8.txt";
$fp = fopen ($filename, "R");
while (! feof ($fp)) {
List ($GB, $utf 8) = Fgetcsv ($fp, 10);
$charset [$GB] = $utf 8;
}
Fclose ($FP);
Read the comparison table above to the array standby

/** gb2312 to Utf-8 **/
function Gb2utf8 ($text, & $charset) {
Extracts the composition of the text, the Chinese character is an element, the continuous non-Chinese character is an element
Preg_match_all ("/(?: [\x80-\xff].) | [\x01-\x7f]+/", $text, $tmp);
$tmp = $tmp [0];
The separation of Chinese characters
$ar = Array_intersect ($tmp, Array_keys ($charset));
Replace encoding
foreach ($ar as $k => $v)
$tmp [$k] = $charset [$v];
Returns the string after the code change
Return join (", $tmp);
}

/** Utf-8 to gb2312 **/
function UTF82GB ($text, & $charset) {
$p = "/[xf0-xf7][x80-xbf]{3}| [Xe0-xef] [X80-XBF] {2}| [XC2-XDF] [x80-xbf]| [x01-x7f]+/];
Preg_match_all ($p, $text, $r);
$utf 8 = Array_flip ($charset);
foreach ($r [0] as $k => $v)
if (Isset ($utf 8[$v]))
$r [0][$k] = $utf 8[$v];
Return join (', $r [0]);
}

Test
$s = Gb2utf8 (' This is the test of the comparison table ', $charset);
Echo utf82gb ($s, $charset);
?>

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.