Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5076489
  • 博文数量: 921
  • 博客积分: 16037
  • 博客等级: 上将
  • 技术积分: 8469
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-05 02:08
文章分类

全部博文(921)

文章存档

2020年(1)

2019年(3)

2018年(3)

2017年(6)

2016年(47)

2015年(72)

2014年(25)

2013年(72)

2012年(125)

2011年(182)

2010年(42)

2009年(14)

2008年(85)

2007年(89)

2006年(155)

分类:

2008-01-03 03:24:25

转换unicode十进制内码为utf-8编码

<?php
/**
* 转换unicode十进制内码为utf-8编码
*/

function u2utf8($c) {
$str="";
if ($c < 0x80) {
  $str.=$c;
} else if ($c < 0x800) {
  $str.=chr(0xC0 | $c>>6);
  $str.=chr(0x80 | $c & 0x3F);
} else if ($c < 0x10000) {
  $str.=chr(0xE0 | $c>>12);
  $str.=chr(0x80 | $c>>6 & 0x3F);
  $str.=chr(0x80 | $c & 0x3F);
} else if ($c < 0x200000) {
  $str.=chr(0xF0 | $c>>18);
  $str.=chr(0x80 | $c>>12 & 0x3F);
  $str.=chr(0x80 | $c>>6 & 0x3F);
  $str.=chr(0x80 | $c & 0x3F);
}
return $str;
}

$source = "中国人民";

preg_match_all("/&#([0-9]+)/",$source,$regs);
print_r($regs);
foreach($regs[1] as $v)
  $source = str_replace("&#$v",iconv("UTF-8","GB2312",u2utf8($v)),$source);
echo $source;
?>


阅读(1909) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~