Location: PHPKode > scripts > Chinese Text Conversion > chinese-text-conversion/class.Chinese.php
<?php

/**
 * ÖÐÎıàÂ뼯ºÏÀà¿â
 *
 * Ä¿Ç°¸ÃÀà¿â¿ÉÒÔʵÏÖ£¬¼òÌåÖÐÎÄ <-> ·±ÌåÖÐÎıàÂ뻥»»£¬¼òÌåÖÐÎÄ¡¢·±ÌåÖÐÎÄ -> Æ´Òôµ¥Ïòת»»£¬
 * ¼òÌåÖÐÎÄ¡¢·±ÌåÖÐÎÄ <-> UTF8 ±àÂëת»»£¬¼òÌåÖÐÎÄ¡¢·±ÌåÖÐÎÄ -> Unicodeµ¥Ïòת»»
 *
 * @×÷Õß         Hessian(hide@address.com)
 * @°æ±¾         1.5
 * @°æȨËùÓÐ     Hessian / NETiS
 * @ʹÓÃÊÚȨ     GPL£¨²»ÄÜÓ¦ÓÃÓÚÈκÎÉÌÒµÓÃ;£¬ÎÞÐë¾­¹ý×÷ÕßͬÒâ¼´¿ÉÐ޸ĴúÂ룬µ«Ð޸ĺóµÄ´úÂë±ØÐë°´ÕÕGPLЭÒé·¢²¼£©
 * @ÌرðÃùл     unknow£¨·±¼òת»»´úÂëƬ¶Ï£©
 * @Æðʼ         2003-04-01
 * @×îºóÐÞ¸Ä     2003-06-06
 * @·ÃÎÊ         ¹«¿ª
 *
 * ¸üмǼ
 *
 * ver 1.6 2003-06-07
 * Ôö¼Ó 
 *
 * ver 1.5 2003-06-06
 * Ôö¼Ó UTF8 ת»»µ½ GB2312¡¢BIG5µÄ¹¦ÄÜ¡£
 *
 * ver 1.4 2003-04-07
 * Ôö¼Ó µ±×ª»»HTMLʱÉ趨Ϊtrue£¬¼´¿É¸Ä±ächarsetµÄÖµ¡£
 *
 * ver 1.3 2003-04-02
 * Ôö¼Ó ·±ÌåÖÐÎÄת»»ÖÁÆ´ÒôµÄ¹¦ÄÜ¡£
 *
 * ver 1.2 2003-04-02
 * ºÏ²¢ ¼òÌå¡¢·±ÌåÖÐÎÄת»»ÖÁUTF8µÄº¯Êý¡£
 * ÐÞ¸Ä ¼òÌåÖÐÎÄת»»ÖÁÆ´ÒôµÄº¯Êý£¬·µ»ØÖµ¸ü¸ÄΪ×Ö·û´®£¬Ã¿Ò»¸öºº×ÖµÄÆ´ÒôÓÿոñ·Ö¿ª
 * Ôö¼Ó ¼òÌåÖÐÎÄת»»Îª UNICODE µÄ¹¦ÄÜ¡£
 * Ôö¼Ó ·±ÌåÖÐÎÄת»»Îª UNICODE µÄ¹¦ÄÜ¡£
 *
 * ver 1.1 2003-04-02
 * Ôö¼Ó OpenFile() º¯Êý£¬Ö§³Ö´ò¿ª±¾µØÎļþºÍÔ¶³ÌÎļþ¡£
 * Ôö¼Ó ¼òÌåÖÐÎÄת»»Îª UTF8 µÄ¹¦ÄÜ¡£
 * Ôö¼Ó ·±ÌåÖÐÎÄת»»Îª UTF8 µÄ¹¦ÄÜ¡£
 *
 * ver 1.0 2003-04-01
 * Ò»¸ö¼¯ºÏÁËÖÐÎļòÌ壬ÖÐÎÄ·±Ìå¶ÔÓ¦¸÷ÖÖ±àÂ뻥»»µÄÀà¿âÒѾ­³õ²½Íê³É¡£
 */
class Chinese
{

	/**
	 * ´æ·Å¼òÌåÖÐÎÄÓëÆ´Òô¶ÔÕÕ±í
	 *
	 * @±äÁ¿ÀàÐÍ  Êý×é
	 * @Æðʼ      1.0
	 * @×îºóÐÞ¸Ä  1.0
	 * @·ÃÎÊ      ÄÚ²¿
	 */
	var $codeNum = array(
						"need"    => false,		// true, false
						"method"  => "hex",		// hex, dec
						);

	/**
	 * ´æ·Å¼òÌåÖÐÎÄÓëÆ´Òô¶ÔÕÕ±í
	 *
	 * @±äÁ¿ÀàÐÍ  Êý×é
	 * @Æðʼ      1.0
	 * @×îºóÐÞ¸Ä  1.0
	 * @·ÃÎÊ      ÄÚ²¿
	 */
	var $pinyin_table = array();

	
	/**
	 * ´æ·Å GB <-> UNICODE ¶ÔÕÕ±íµÄÄÚÈÝ
	 * @±äÁ¿ÀàÐÍ  
	 * @Æðʼ      1.1
	 * @×îºóÐÞ¸Ä  1.2
	 * @·ÃÎÊ      ÄÚ²¿
	 */
	var $unicode_table = array();

	/**
	 * ·ÃÎÊÖÐÎÄ·±¼ò»¥»»±íµÄÎļþÖ¸Õë
	 *
	 * @±äÁ¿ÀàÐÍ  ¶ÔÏó
	 * @Æðʼ      1.0
	 * @×îºóÐÞ¸Ä  1.0
	 * @·ÃÎÊ      ÄÚ²¿
	 */
	var $ctf;

	/**
	 * µÈ´ýת»»µÄ×Ö·û´®
	 * @±äÁ¿ÀàÐÍ
	 * @Æðʼ      1.0
	 * @×îºóÐÞ¸Ä  1.0
	 * @·ÃÎÊ      ÄÚ²¿
	 */
	var $SourceText = "";

	/**
	 * Chinese µÄÔËÐÐÅäÖÃ
	 *
	 * @±äÁ¿ÀàÐÍ  Êý×é
	 * @Æðʼ      1.0
	 * @×îºóÐÞ¸Ä  1.2
	 * @·ÃÎÊ      ¹«¿ª
	 */
	var $config  =  array(
		'codetable_dir'         => "./config/",           //  ´æ·Å¸÷ÖÖÓïÑÔ»¥»»±íµÄĿ¼
		'SourceLang'            => '',                    //  ×Ö·ûµÄÔ­±àÂë
		'TargetLang'            => '',                    //  ת»»ºóµÄ±àÂë
		'GBtoBIG5_table'        => 'gb-big5.table',       //  ¼òÌåÖÐÎÄת»»Îª·±ÌåÖÐÎĵĶÔÕÕ±í
		'BIG5toGB_table'        => 'big5-gb.table',       //  ·±ÌåÖÐÎÄת»»Îª¼òÌåÖÐÎĵĶÔÕÕ±í
		'GBtoPinYin_table'      => 'gb-pinyin.table',     //  ¼òÌåÖÐÎÄת»»ÎªÆ´ÒôµÄ¶ÔÕÕ±í
		'GBtoUnicode_table'     => 'gb-unicode.table',    //  ¼òÌåÖÐÎÄת»»ÎªUNICODEµÄ¶ÔÕÕ±í
		'BIG5toUnicode_table'   => 'big5-unicode.table'   //  ·±ÌåÖÐÎÄת»»ÎªUNICODEµÄ¶ÔÕÕ±í
	);

	/**
	 * Chinese µÄϤ¹¹º¯Êý
	 *
	 * Ïêϸ˵Ã÷
	 * @ÐβΠ     ×Ö·û´® $SourceLang ΪÐèҪת»»µÄ×Ö·û´®µÄÔ­±àÂë
	 *            ×Ö·û´® $TargetLang Ϊת»»µÄÄ¿±ê±àÂë
	 *            ×Ö·û´® $SourceText ΪµÈ´ýת»»µÄ×Ö·û´®
	 *
	 * @Æðʼ      1.0
	 * @×îºóÐÞ¸Ä  1.2
	 * @·ÃÎÊ      ¹«¿ª
	 * @·µ»ØÖµ    ÎÞ
	 * @throws
	 */
	function Chinese( $SourceLang , $TargetLang , $SourceString='')
	{
		if ($SourceLang != '') {
		    $this->config['SourceLang'] = $SourceLang;
		}

		if ($TargetLang != '') {
		    $this->config['TargetLang'] = $TargetLang;
		}

		if ($SourceString != '') {
		    $this->SourceText = $SourceString;
		}

		$this->OpenTable();
	} // ½áÊø Chinese µÄϤ¹¹º¯Êý


	/**
	 * ½« 16 ½øÖÆת»»Îª 2 ½øÖÆ×Ö·û
	 *
	 * Ïêϸ˵Ã÷
	 * @ÐβΠ     $hexdata Ϊ16½øÖƵıàÂë
	 * @Æðʼ      1.5
	 * @×îºóÐÞ¸Ä  1.5
	 * @·ÃÎÊ      ÄÚ²¿
	 * @·µ»Ø      ×Ö·û´®
	 * @throws    
	 */
	function _hex2bin( $hexdata )
	{
		for ( $i=0; $i<strlen($hexdata); $i+=2 )
			$bindata.=chr(hexdec(substr($hexdata,$i,2)));
//			$bindata.=pack("C", hexdec(substr($hexdata,$i,2)));

		return $bindata;
	}


	/**
	 * ´ò¿ª¶ÔÕÕ±í
	 *
	 * Ïêϸ˵Ã÷
	 * @ÐβΠ     
	 * @Æðʼ      1.3
	 * @×îºóÐÞ¸Ä  1.3
	 * @·ÃÎÊ      ÄÚ²¿
	 * @·µ»Ø      ÎÞ
	 * @throws    
	 */
	function OpenTable()
	{
	    
		// ¼ÙÈçÔ­±àÂëΪ¼òÌåÖÐÎĵĻ°
		if ($this->config['SourceLang']=="GB2312") {

			// ¼ÙÈçת»»Ä¿±ê±àÂëΪ·±ÌåÖÐÎĵĻ°
			if ($this->config['TargetLang'] == "BIG5") {
				$this->ctf = fopen($this->config['codetable_dir'].$this->config['GBtoBIG5_table'], "r");
				if (is_null($this->ctf)) {
					echo "´ò¿ª´ò¿ª×ª»»±íÎļþʧ°Ü£¡";
					exit;
				}
			}

			// ¼ÙÈçת»»Ä¿±ê±àÂëΪƴÒôµÄ»°
			if ($this->config['TargetLang'] == "PinYin") {
				$tmp = @file($this->config['codetable_dir'].$this->config['GBtoPinYin_table']);
				if (!$tmp) {
					echo "´ò¿ª´ò¿ª×ª»»±íÎļþʧ°Ü£¡";
					exit;
				}
				//
				$i = 0;
				for ($i=0; $i<count($tmp); $i++) {
					$tmp1 = explode("	", $tmp[$i]);
					$this->pinyin_table[$i]=array($tmp1[0],$tmp1[1]);
				}
			}

			// ¼ÙÈçת»»Ä¿±ê±àÂëΪ UTF8 µÄ»°
			if ($this->config['TargetLang'] == "UTF8") {
				$tmp = @file($this->config['codetable_dir'].$this->config['GBtoUnicode_table']);
				if (!$tmp) {
					echo "´ò¿ª´ò¿ª×ª»»±íÎļþʧ°Ü£¡";
					exit;
				}
				$this->unicode_table = array();
				while(list($key,$value)=each($tmp))
					$this->unicode_table[hexdec(substr($value,0,6))]=substr($value,7,6);
			}

			// ¼ÙÈçת»»Ä¿±ê±àÂëΪ UNICODE µÄ»°
			if ($this->config['TargetLang'] == "UNICODE") {
				$tmp = @file($this->config['codetable_dir'].$this->config['GBtoUnicode_table']);
				if (!$tmp) {
					echo "´ò¿ª´ò¿ª×ª»»±íÎļþʧ°Ü£¡";
					exit;
				}
				$this->unicode_table = array();
				while(list($key,$value)=each($tmp))
					$this->unicode_table[hexdec(substr($value,0,6))]=substr($value,7,6);
			}
		}

		// ¼ÙÈçÔ­±àÂëΪ·±ÌåÖÐÎĵĻ°
		if ($this->config['SourceLang']=="BIG5") {
			// ¼ÙÈçת»»Ä¿±ê±àÂëΪ¼òÌåÖÐÎĵĻ°
			if ($this->config['TargetLang'] == "GB2312") {
				$this->ctf = fopen($this->config['codetable_dir'].$this->config['BIG5toGB_table'], "r");
				if (is_null($this->ctf)) {
					echo "´ò¿ª´ò¿ª×ª»»±íÎļþʧ°Ü£¡";
					exit;
				}
			}
			// ¼ÙÈçת»»Ä¿±ê±àÂëΪ UTF8 µÄ»°
			if ($this->config['TargetLang'] == "UTF8") {
				$tmp = @file($this->config['codetable_dir'].$this->config['BIG5toUnicode_table']);
				if (!$tmp) {
					echo "´ò¿ª´ò¿ª×ª»»±íÎļþʧ°Ü£¡";
					exit;
				}
				$this->unicode_table = array();
				while(list($key,$value)=each($tmp))
					$this->unicode_table[hexdec(substr($value,0,6))]=substr($value,7,6);
			}

			// ¼ÙÈçת»»Ä¿±ê±àÂëΪ UNICODE µÄ»°
			if ($this->config['TargetLang'] == "UNICODE") {
				$tmp = @file($this->config['codetable_dir'].$this->config['BIG5toUnicode_table']);
				if (!$tmp) {
					echo "´ò¿ª´ò¿ª×ª»»±íÎļþʧ°Ü£¡";
					exit;
				}
				$this->unicode_table = array();
				while(list($key,$value)=each($tmp))
					$this->unicode_table[hexdec(substr($value,0,6))]=substr($value,9,4);
			}

			// ¼ÙÈçת»»Ä¿±ê±àÂëΪƴÒôµÄ»°
			if ($this->config['TargetLang'] == "PinYin") {
				$tmp = @file($this->config['codetable_dir'].$this->config['GBtoPinYin_table']);
				if (!$tmp) {
					echo "´ò¿ª´ò¿ª×ª»»±íÎļþʧ°Ü£¡";
					exit;
				}
				//
				$i = 0;
				for ($i=0; $i<count($tmp); $i++) {
					$tmp1 = explode("	", $tmp[$i]);
					$this->pinyin_table[$i]=array($tmp1[0],$tmp1[1]);
				}
			}
		}

		// ¼ÙÈçÔ­±àÂëΪ UTF8 µÄ»°
		if ($this->config['SourceLang']=="UTF8") {

			// ¼ÙÈçת»»Ä¿±ê±àÂëΪ GB2312 µÄ»°
			if ($this->config['TargetLang'] == "GB2312") {
				$tmp = @file($this->config['codetable_dir'].$this->config['GBtoUnicode_table']);
				if (!$tmp) {
					echo "´ò¿ª´ò¿ª×ª»»±íÎļþʧ°Ü£¡";
					exit;
				}
				$this->unicode_table = array();
				while(list($key,$value)=each($tmp))
					$this->unicode_table[hexdec(substr($value,7,6))]=substr($value,0,6);
			}

			// ¼ÙÈçת»»Ä¿±ê±àÂëΪ BIG5 µÄ»°
			if ($this->config['TargetLang'] == "BIG5") {
				$tmp = @file($this->config['codetable_dir'].$this->config['BIG5toUnicode_table']);
				if (!$tmp) {
					echo "´ò¿ª´ò¿ª×ª»»±íÎļþʧ°Ü£¡";
					exit;
				}
				$this->unicode_table = array();
				while(list($key,$value)=each($tmp))
					$this->unicode_table[hexdec(substr($value,7,6))]=substr($value,0,6);
			}
		}

	} // ½áÊø OpenTable º¯Êý

	/**
	 * ´ò¿ª±¾µØ»òÕßÔ¶³ÌµÄÎļþ
	 *
	 * Ïêϸ˵Ã÷
	 * @ÐβΠ     ×Ö·û´® $position ΪÐèÒª´ò¿ªµÄÎļþÃû³Æ£¬Ö§³Ö´ø·¾¶»òURL
	 *            ²¼¶ûÖµ $isHTML Ϊ´ýת»»µÄÎļþÊÇ·ñΪhtmlÎļþ
	 * @Æðʼ      1.1
	 * @×îºóÐÞ¸Ä  1.1
	 * @·ÃÎÊ      ¹«¿ª
	 * @·µ»Ø      ÎÞ
	 * @throws    
	 */
	function OpenFile( $position , $isHTML=false )
	{
	    $tempcontent = @file($position);

		if (!$tempcontent) {
		    echo "´ò¿ªÎļþʧ°Ü£¡";
			exit;
		}

		$this->SourceText = implode("",$tempcontent);

		if ($isHTML) {
			$this->SourceText = eregi_replace( "charset=".$this->config['SourceLang'] , "charset=".$this->config['TargetLang'] , $this->SourceText);

			$this->SourceText = eregi_replace("\n", "", $this->SourceText);

			$this->SourceText = eregi_replace("\r", "", $this->SourceText);
		}
	} // ½áÊø OpenFile º¯Êý

	/**
	 * ´ò¿ª±¾µØ»òÕßÔ¶³ÌµÄÎļþ
	 *
	 * Ïêϸ˵Ã÷
	 * @ÐβΠ     ×Ö·û´® $position ΪÐèÒª´ò¿ªµÄÎļþÃû³Æ£¬Ö§³Ö´ø·¾¶»òURL
	 * @Æðʼ      1.1
	 * @×îºóÐÞ¸Ä  1.1
	 * @·ÃÎÊ      ¹«¿ª
	 * @·µ»Ø      ÎÞ
	 * @throws    
	 */
	function SiteOpen( $position )
	{
	    $tempcontent = @file($position);

		if (!$tempcontent) {
		    echo "´ò¿ªÎļþʧ°Ü£¡";
			exit;
		}

		// ½«Êý×éµÄËùÓÐÄÚÈÝת»»Îª×Ö·û´®
		$this->SourceText = implode("",$tempcontent);

		$this->SourceText = eregi_replace( "charset=".$this->config['SourceLang'] , "charset=".$this->config['TargetLang'] , $this->SourceText);


//		ereg(href="css/dir.css"
	} // ½áÊø OpenFile º¯Êý

	/**
	 * ÉèÖñäÁ¿µÄÖµ
	 *
	 * Ïêϸ˵Ã÷
	 * @ÐβÎ
	 * @Æðʼ      1.0
	 * @×îºóÐÞ¸Ä  1.0
	 * @·ÃÎÊ      ¹«¿ª
	 * @·µ»ØÖµ    ÎÞ
	 * @throws
	 */
	function setvar( $parameter , $value )
	{
		if(!trim($parameter))
			return $parameter;

		$this->config[$parameter] = $value;

	} // ½áÊø setvar º¯Êý

	/**
	 * ½«¼òÌå¡¢·±ÌåÖÐÎÄµÄ UNICODE ±àÂëת»»Îª UTF8 ×Ö·û
	 *
	 * Ïêϸ˵Ã÷
	 * @ÐβΠ     Êý×Ö $c ¼òÌåÖÐÎĺº×ÖµÄUNICODE±àÂëµÄ10½øÖÆ
	 * @Æðʼ      1.1
	 * @×îºóÐÞ¸Ä  1.2
	 * @·ÃÎÊ      ÄÚ²¿
	 * @·µ»Ø      ×Ö·û´®
	 * @throws    
	 */
	function CHSUtoUTF8($c)
	{
		$str="";

		if ($c < 0x80) {
			$str.=$c;
		}

		else if ($c < 0x800) {
			$str.=(0xC0 | $c>>6);
			$str.=(0x80 | $c & 0x3F);
		}

		else if ($c < 0x10000) {
			$str.=(0xE0 | $c>>12);
			$str.=(0x80 | $c>>6 & 0x3F);
			$str.=(0x80 | $c & 0x3F);
		}

		else if ($c < 0x200000) {
			$str.=(0xF0 | $c>>18);
			$str.=(0x80 | $c>>12 & 0x3F);
			$str.=(0x80 | $c>>6 & 0x3F);
			$str.=(0x80 | $c & 0x3F);
		}

		return $str;
	} // ½áÊø CHSUtoUTF8 º¯Êý
	
	/**
	 * ¼òÌå¡¢·±ÌåÖÐÎÄ <-> UTF8 »¥Ïàת»»µÄº¯Êý
	 *
	 * Ïêϸ˵Ã÷
	 * @ÐβΠ     
	 * @Æðʼ      1.1
	 * @×îºóÐÞ¸Ä  1.5
	 * @·ÃÎÊ      ÄÚ²¿
	 * @·µ»Ø      ×Ö·û´®
	 * @throws    
	 */
	function CHStoUTF8(){

		if ($this->config["SourceLang"]=="BIG5" || $this->config["SourceLang"]=="GB2312") {
			$ret="";

			while($this->SourceText){

				if(ord(substr($this->SourceText,0,1))>127){

					if ($this->config["SourceLang"]=="BIG5") {
						$utf8=$this->CHSUtoUTF8(hexdec($this->unicode_table[hexdec(bin2hex(substr($this->SourceText,0,2)))]));
					}
					if ($this->config["SourceLang"]=="GB2312") {
						$utf8=$this->CHSUtoUTF8(hexdec($this->unicode_table[hexdec(bin2hex(substr($this->SourceText,0,2)))-0x8080]));
					}
					for($i=0;$i<strlen($utf8);$i+=3)
						$ret.=chr(substr($utf8,$i,3));

					$this->SourceText=substr($this->SourceText,2,strlen($this->SourceText));
				}
				
				else{
					$ret.=substr($this->SourceText,0,1);
					$this->SourceText=substr($this->SourceText,1,strlen($this->SourceText));
				}
			}
			$this->unicode_table = array();
			$this->SourceText = "";
			return $ret;
		}

		if ($this->config["SourceLang"]=="UTF8") {
			$out = "";
			$len = strlen($this->SourceText);
			$i = 0;
			while($i < $len) {
				$c = ord( substr( $this->SourceText, $i++, 1 ) );
				switch($c >> 4)
				{ 
					case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
						// 0xxxxxxx
						$out .= substr( $this->SourceText, $i-1, 1 );
					break;
					case 12: case 13:
						// 110x xxxx   10xx xxxx
						$char2 = ord( substr( $this->SourceText, $i++, 1 ) );
						$char3 = $this->unicode_table[(($c & 0x1F) << 6) | ($char2 & 0x3F)];

						if ($this->config["TargetLang"]=="GB2312")
							$out .= $this->_hex2bin( dechex(  $char3 + 0x8080 ) );

						if ($this->config["TargetLang"]=="BIG5")
							$out .= $this->_hex2bin( $char3 );
					break;
					case 14:
						// 1110 xxxx  10xx xxxx  10xx xxxx
						$char2 = ord( substr( $this->SourceText, $i++, 1 ) );
						$char3 = ord( substr( $this->SourceText, $i++, 1 ) );
						$char4 = $this->unicode_table[(($c & 0x0F) << 12) | (($char2 & 0x3F) << 6) | (($char3 & 0x3F) << 0)];

						if ($this->config["TargetLang"]=="GB2312")
							$out .= $this->_hex2bin( dechex ( $char4 + 0x8080 ) );

						if ($this->config["TargetLang"]=="BIG5")
							$out .= $this->_hex2bin( $char4 );
					break;
				}
			}

			// ·µ»Ø½á¹û
			return $out;
		}
	} // ½áÊø CHStoUTF8 º¯Êý


	/**
	 * ¼òÌå¡¢·±ÌåÖÐÎÄת»»Îª UNICODE±àÂë
	 *
	 * Ïêϸ˵Ã÷
	 * @ÐβΠ     
	 * @Æðʼ      1.2
	 * @×îºóÐÞ¸Ä  1.2
	 * @·ÃÎÊ      ÄÚ²¿
	 * @·µ»Ø      ×Ö·û´®
	 * @throws    
	 */
	function CHStoUNICODE()
	{

		$utf="";

		$utf .= $this->_hex2bin( feff );

		while($this->SourceText)
		{
			if (ord(substr($this->SourceText,0,1))>127)
			{

				if ($this->config["SourceLang"]=="GB2312") {
					$temp = hexdec( $this->unicode_table[hexdec( bin2hex( substr( $this->SourceText,0,2 ) ) )- 0x8080] );

					if ( $this->codeNum["need"] ) {

						if ( $this->codeNum["method"] == "hex" )
							$utf .= dechex( $temp )."\n";

						if ( $this->codeNum["method"] == "dec" )
							$utf .= $temp."\n";
					}
					else {
						$utf .= $this->_hex2bin( dechex( $temp ) );
//						$utf .= "&#x".dechex( $temp ).";";
					}
				}

				if ($this->config["SourceLang"]=="BIG5")
					$utf.="&#x".$this->unicode_table[hexdec(bin2hex(substr($this->SourceText,0,2)))].";";

				$this->SourceText=substr($this->SourceText,2,strlen($this->SourceText));
			}
			else
			{
				$utf.=substr($this->SourceText,0,1);
				$this->SourceText=substr($this->SourceText,1,strlen($this->SourceText));
			}
		}
		return $utf;
	} // ½áÊø CHStoUNICODE º¯Êý

	/**
	 * ¼òÌåÖÐÎÄ <-> ·±ÌåÖÐÎÄ »¥Ïàת»»µÄº¯Êý
	 *
	 * Ïêϸ˵Ã÷
	 * @Æðʼ      1.0
	 * @·ÃÎÊ      ÄÚ²¿
	 * @·µ»ØÖµ    ¾­¹ý±àÂëµÄutf8×Ö·û
	 * @throws
	 */
	function GB2312toBIG5()
	{
		// »ñÈ¡µÈ´ýת»»µÄ×Ö·û´®µÄ×ܳ¤¶È
		$max=strlen($this->SourceText)-1;

		for($i=0;$i<$max;$i++){

			$h=ord($this->SourceText[$i]);

			if($h>=160){

				$l=ord($this->SourceText[$i+1]);

				if($h==161 && $l==64){
					$gb="  ";
				}
				else{
					fseek($this->ctf,($h-160)*510+($l-1)*2);
					$gb=fread($this->ctf,2);
				}

				$this->SourceText[$i]=$gb[0];
				$this->SourceText[$i+1]=$gb[1];
				$i++;
			}
		}
		fclose($this->ctf);

		// ½«×ª»»ºóµÄ½á¹û¸³Óè $result;
		$result = $this->SourceText;

		// Çå¿Õ $thisSourceText
		$this->SourceText = "";

		// ·µ»Øת»»½á¹û
		return $result;
	} // ½áÊø GB2312toBIG5 º¯Êý

	/**
	 * ¸ù¾ÝËùµÃµ½µÄ±àÂëËÑÑ°Æ´Òô
	 *
	 * Ïêϸ˵Ã÷
	 * @Æðʼ      1.0
	 * @×îºóÐÞ¸Ä  1.0
	 * @·ÃÎÊ      ÄÚ²¿
	 * @·µ»ØÖµ    ×Ö·û´®
	 * @throws
	 */
	function PinYinSearch($num){

		if($num>0&&$num<160){
			return chr($num);
		}

		elseif($num<-20319||$num>-10247){
			return "";
		}

		else{

			for($i=count($this->pinyin_table)-1;$i>=0;$i--){
				if($this->pinyin_table[$i][1]<=$num)
					break;
			}

			return $this->pinyin_table[$i][0];
		}
	} // ½áÊø PinYinSearch º¯Êý

	/**
	 * ¼òÌå¡¢·±ÌåÖÐÎÄ -> Æ´Òô ת»»
	 *
	 * Ïêϸ˵Ã÷
	 * @Æðʼ      1.0
	 * @×îºóÐÞ¸Ä  1.3
	 * @·ÃÎÊ      ÄÚ²¿
	 * @·µ»ØÖµ    ×Ö·û´®£¬Ã¿¸öÆ´ÒôÓÿոñ·Ö¿ª
	 * @throws
	 */
	function CHStoPinYin(){
		if ( $this->config['SourceLang']=="BIG5" ) {
			$this->ctf = fopen($this->config['codetable_dir'].$this->config['BIG5toGB_table'], "r");
			if (is_null($this->ctf)) {
				echo "´ò¿ª´ò¿ª×ª»»±íÎļþʧ°Ü£¡";
				exit;
			}

			$this->SourceText = $this->GB2312toBIG5();
			$this->config['TargetLang'] = "PinYin";
		}

		$ret = array();
		$ri = 0;
		for($i=0;$i<strlen($this->SourceText);$i++){

			$p=ord(substr($this->SourceText,$i,1));

			if($p>160){
				$q=ord(substr($this->SourceText,++$i,1));
				$p=$p*256+$q-65536;
			}

			$ret[$ri]=$this->PinYinSearch($p);
			$ri = $ri + 1;
		}

		// Çå¿Õ $this->SourceText
		$this->SourceText = "";

		$this->pinyin_table = array();

		// ·µ»Øת»»ºóµÄ½á¹û
		return implode(" ", $ret);
	} // ½áÊø CHStoPinYin º¯Êý

	/**
	 * Êä³öת»»½á¹û
	 *
	 * Ïêϸ˵Ã÷
	 * @ÐβÎ
	 * @Æðʼ      1.0
	 * @×îºóÐÞ¸Ä  1.2
	 * @·ÃÎÊ      ¹«¿ª
	 * @·µ»Ø      ×Ö·û»»
	 * @throws
	 */
	function ConvertIT()
	{
		// ÅжÏÊÇ·ñΪÖÐÎÄ·±¡¢¼òת»»
		if ( ($this->config['SourceLang']=="GB2312" || $this->config['SourceLang']=="BIG5") && ($this->config['TargetLang']=="GB2312" || $this->config['TargetLang']=="BIG5") ) {
			return $this->GB2312toBIG5();
		}

		// ÅжÏÊÇ·ñΪ¼òÌåÖÐÎÄÓëÆ´Òôת»»
		if ( ($this->config['SourceLang']=="GB2312" || $this->config['SourceLang']=="BIG5") && $this->config['TargetLang']=="PinYin" ) {
			return $this->CHStoPinYin();
		}

		// ÅжÏÊÇ·ñΪ¼òÌå¡¢·±ÌåÖÐÎÄÓëUTF8ת»»
		if ( ($this->config['SourceLang']=="GB2312" || $this->config['SourceLang']=="BIG5" || $this->config['SourceLang']=="UTF8") && ($this->config['TargetLang']=="UTF8" || $this->config['TargetLang']=="GB2312" || $this->config['TargetLang']=="BIG5") ) {
			return $this->CHStoUTF8();
		}

		// ÅжÏÊÇ·ñΪ¼òÌå¡¢·±ÌåÖÐÎÄÓëUNICODEת»»
		if ( ($this->config['SourceLang']=="GB2312" || $this->config['SourceLang']=="BIG5") && $this->config['TargetLang']=="UNICODE" ) {
			return $this->CHStoUNICODE();
		}

	} // ½áÊø ConvertIT º¯Êý

} // ½áÊøÀà¿â

/**
*/

?>
Return current item: Chinese Text Conversion