php 不用iconv()函数 UTF8转GB2312字符串编码

添加时间:10-07-16 所属分类:PHP工具与代码
  请先下载gb2utf8文件,[url:3frdyb2d]http://www.11jn.com/gb2utf8.rar[/url:3frdyb2d]
[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]

前篇:php中gb2312跟unicode码的转换 后篇:PHP 不用iconv()函数 gbk转到unicode(utf8)
发表我的评论


推荐文章   高温来袭去哪儿避暑? 六处人防纳凉点成...   多地出现新型网络骗局:以美女形象卖“...   员工与离职员工吃散伙饭被解雇!老板:...   凡有母亲的地方就有奇迹   为救儿子 女子每日剪万斤钢筋
随机文章   用php将字符串转换成数组   php去掉字符串的最后一个字符substr()的...   php提取字符串中的数字   php判断比较两个数组中的数据   PHP打开txt,写入Mysql,涉及文件读取,...
广告

其他推荐

因为本站所有内容均转载自其它媒体,本意为公众提供免费服务,但并不代表本网赞同其观点,也不能对其真实性验证负责,如稿件版权单位或个人不想在本网发布,请与我联系,本人会立即将其撤除,谢谢.联系方式:atseashawk@163.com QQ:99289555