5 http://code.google.com/p/webtool123/downloads/detail?name=str_chinese.php&can=2&q=
7 *mbstring are required*
8 Convert to Traditional Chinese
9 Convert to Simplified Chinese
13 Copyright 2007 francissem@gmail.com
14 Licensed under the Apache License, Version 2.0 (the "License");
15 you may not use this file except in compliance with the License.
16 You may obtain a copy of the License at http://www.apache.org/licenses/
17 LICENSE-2.0 Unless required by applicable law or agreed to in writing,
18 software distributed under the License is distributed on an "AS IS" BASIS,
19 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 See the License for the specific language governing permissions and
21 limitations under the License.
23 ---------------------------------------------------------------------------
27 if (!function_exists('array_combine')) {
30 function array_combine($arr1,$arr2) {
32 foreach($arr1 as $key1 => $value1) {
33 $out[$value1] = $arr2[$key1];
38 $GLOBALS['Text_zhDetect']['t2smap'] = array(
2285 $GLOBALS['Text_zhDetect']['exc'] = array(
2568 function guess($str, $def = 'T')
2570 $_t2smap = $GLOBALS['Text_zhDetect']['t2smap'];
2571 if (empty($GLOBALS['Text_zhDetect']['s2tmap'])) {
2572 $this->buildS2Tmap();
2574 $_s2tmap = $GLOBALS['Text_zhDetect']['s2tmap'];
2577 for($i=0;$i<mb_strlen($str);$i++){
2578 $tc=mb_substr($str,$i,1);
2579 $tc_n = $this->uniord($tc);
2580 if (!isset($_s2tmap[$tc_n]) && !isset($_t2smap[$tc_n])) {
2584 if (isset($_s2tmap[$tc_n]) && isset($_t2smap[$tc_n])) {
2588 if (!isset($_s2tmap[$tc_n]) && isset($_t2smap[$tc_n])) {
2591 if (isset($_s2tmap[$tc_n]) && !isset($_t2smap[$tc_n])) {
2594 // should not get here..
2598 function buildS2Tmap()
2600 $GLOBALS['Text_zhDetect']['s2tmap']=array_combine(
2601 array_values($GLOBALS['Text_zhDetect']['t2smap']),array_keys($GLOBALS['Text_zhDetect']['t2smap'])
2603 foreach($GLOBALS['Text_zhDetect']['exc'] as $code) {
2604 unset( $GLOBALS['Text_zhDetect']['s2tmap'][$code]);
2609 function uniord($c) {
2613 } else if ($h < 0xC2) {
2615 } else if ($h <= 0xDF) {
2616 return ($h & 0x1F) << 6 | (ord($c[1]) & 0x3F);
2617 } else if ($h <= 0xEF && strlen($c) > 2) {
2618 return ($h & 0x0F) << 12 | (ord($c[1]) & 0x3F) << 6
2619 | (ord($c[2]) & 0x3F);
2620 } else if ($h <= 0xF4 && strlen($c) > 3) {
2621 return ($h & 0x0F) << 18 | (ord($c[1]) & 0x3F) << 12
2622 | (ord($c[2]) & 0x3F) << 6
2623 | (ord($c[3]) & 0x3F);
2630 function unichr($c) {
2633 } else if ($c <= 0x7FF) {
2634 return chr(0xC0 | $c >> 6) . chr(0x80 | $c & 0x3F);
2635 } else if ($c <= 0xFFFF) {
2636 return chr(0xE0 | $c >> 12) . chr(0x80 | $c >> 6 & 0x3F)
2637 . chr(0x80 | $c & 0x3F);
2638 } else if ($c <= 0x10FFFF) {
2639 return chr(0xF0 | $c >> 18) . chr(0x80 | $c >> 12 & 0x3F)
2640 . chr(0x80 | $c >> 6 & 0x3F)
2641 . chr(0x80 | $c & 0x3F);