haidong 发表于 2010-2-24 13:34:35

GB2312->UTF-8 转换函数

<br>                   以下是一段gb2312 -&gt; UTF-8 的函数,西西,我抄了unicode -&gt; UTF-8 的算法,所以实际比GB2312-&gt;unicode <BR>没多多少东西,请大家注意调用gb2utf8()时不能中英文混用 <BR><BR>程序需要的GB2312.txt文件 <BR><BR>gb2utf8.php <BR>&lt;? <BR><BR>//Program writen by sadly www.phpx.com <BR><BR>function gb2utf8($gb) <BR>{ <BR>if(!trim($gb)) <BR>return $gb; <BR>$filename="gb2312.txt"; <BR>$tmp=file($filename); <BR>$codetable=array(); <BR>while(list($key,$value)=each($tmp)) <BR>$codetable=substr($value,7,6); <BR><BR>$utf8=""; <BR>while($gb) <BR>{ <BR>if (ord(substr($gb,0,1))&gt;127) <BR>{ <BR>$this=substr($gb,0,2); <BR>$gb=substr($gb,2,strlen($gb)); <BR>$utf8.=u2utf8(hexdec($codetable)); <BR>} <BR>else <BR>{ <BR>$gb=substr($gb,1,strlen($gb)); <BR>$utf8.=u2utf8(substr($gb,0,1)); <BR>} <BR>} <BR><BR>$ret=""; <BR>for($i=0;$i&lt;strlen($utf8);$i+=3) <BR>$ret.=chr_(substr($utf8,$i,3)); <BR><BR>return $ret; <BR>} <BR><BR>function u2utf8($c) <BR>{ <BR>for($i=0;$i&lt;count($c);$i++) <BR>$str=""; <BR>if ($c &lt; 0x80) { <BR>$str.=$c; <BR>} <BR>else if ($c &lt; 0x800) { <BR>$str.=(0xC0 | $c&gt;&gt;6); <BR>$str.=(0x80 | $c &amp; 0x3F); <BR>} <BR>else if ($c &lt; 0x10000) { <BR>$str.=(0xE0 | $c&gt;&gt;12); <BR>$str.=(0x80 | $c&gt;&gt;6 &amp; 0x3F); <BR>$str.=(0x80 | $c &amp; 0x3F); <BR>} <BR>else if ($c &lt; 0x200000) { <BR>$str.=(0xF0 | $c&gt;&gt;18); <BR>$str.=(0x80 | $c&gt;&gt;12 &amp; 0x3F); <BR>$str.=(0x80 | $c&gt;&gt;6 &amp; 0x3F); <BR>$str.=(0x80 | $c &amp; 0x3F); <BR>} <BR>return $str; <BR>} <BR>?&gt; <BR><BR><BR><BR>调用举例:通过GD输出 "中国" 两个汉字 <BR>example.php <BR><BR>&lt;? <BR>//Header("Content-type: image/gif"); <BR>$im = imagecreate(400,300); <BR>$bkg = ImageColorAllocate($im, 0,0,0); <BR>$clr = ImageColorAllocate($im, 255,255,255); <BR>$fnt = "d:/winnt/fonts/simhei.ttf"; <BR>include("gb2utf8.php"); <BR>$str = gb2utf8("中国"); <BR>ImageTTFText($im, 20, 0, 10, 20, $clr, $fnt, $str); <BR>ImageGif($im); <BR>ImageDestroy($im); <BR>?&gt;                   <br><br>
页: [1]
查看完整版本: GB2312->UTF-8 转换函数