php 不用iconv()函数 UTF8转GB2312字符串编码
[color=#FF0000]下载后,请解压gb2utf8.rar文件获得gb2utf8.txt文件,并与你的PHP程序文件放在同一目录下。[/color]
[code]
<?php
function utf82gb($utf8)
{
static $utf82gbcodetable ;
// if ( function_exists('iconv') )
// return $message = iconv("UTF-8", "gb2312" ,$utf8) ;
if ( ! $utf82gbcodetable ) {
$codetablefile = file("gb2utf8.txt",1);
$utf82gbcodetable=array();
while(list($key,$value)=each($codetablefile))
$utf82gbcodetable[substr($value,5,substr($value,3,1))]=substr($value,0,2);
}
for($gb=""; $utf8!=''; )
{
if (ord(substr($utf8,0,1))>224) { # 三字节
$gb .= $utf82gbcodetable[substr($utf8,0,3)];
$utf8=substr($utf8,3);
} elseif ( ord(substr($utf8,0,1))>127 ) {
$gb .= $utf82gbcodetable[substr($utf8,0,2)];
$utf8=substr($utf8,2);
} else {
$gb.=substr($utf8,0,1);
$utf8=substr($utf8,1);
}
}
return $gb;
}
$file= utf82gb($file);
// UTF8转GB2312字符串编码END
?>[/code]