Heim > Artikel > Backend-Entwicklung > php各种编码转换实现代码(1/6)_PHP教程
我们提供了几种编码转换如有UTF-8 转GB编码 GB转UTF-8编码 Unicode转utf8 unicode url编码转gbk编码函数 GB码转换成Big5码 Big5码转换成GB码 utf8转Unicode等哦。
我们提供了几种编码转换如有utf-8 转gb编码 gb转utf-8编码 unicode转utf8 unicode url编码转gbk编码函数 gb码转换成big5码 big5码转换成gb码 utf8转unicode等哦。
*/
$uc2gbtable = $codetable = $big5_data = $gb_data = '';
$gbkunidic = null;//utf-8 转gb编码
function utf82gb($utfstr)
{
if(function_exists('iconv'))
{
return iconv('utf-8','gbk//ignore',$utfstr);
}
global $uc2gbtable;
$okstr = "";
if(trim($utfstr)=="")
{
return $utfstr;
}
if(empty($uc2gbtable))
{
$filename = dedeinc."/data/gb2312-utf8.dat";
$fp = fopen($filename,"r");
while($l = fgets($fp,15))
{
$uc2gbtable[hexdec(substr($l, 7, 6))] = hexdec(substr($l, 0, 6));
}
fclose($fp);
}
$okstr = "";
$ulen = strlen($utfstr);
for($i=0;$i {
$c = $utfstr[$i];
$cb = decbin(ord($utfstr[$i]));
if(strlen($cb)==8)
{
$csize = strpos(decbin(ord($cb)),"0");
for($j=0;$j {
$i++; $c .= $utfstr[$i];
}
$c = utf82u($c);
if(isset($uc2gbtable[$c]))
{
$c = dechex($uc2gbtable[$c]+0x8080);
$okstr .= chr(hexdec($c[0].$c[1])).chr(hexdec($c[2].$c[3]));
}
else
{
$okstr .= "".$c.";";
}
}
else
{
$okstr .= $c;
}
}
$okstr = trim($okstr);
return $okstr;
}
1 2 3 4 5 6