The function of a copy of Gb2312.txt (184799 bytes) does appear to be a bit too large and is also converted by 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);
}
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.