PHP实现Unicode编码相互转换的方法示例

本文实例讲述了PHP实现Unicode编码相互转换的方法。分享给大家供大家参考,具体如下:

<?php
/**
* $str 原始中文字符串
* $encoding 原始字符串的编码,默认utf-8
* $prefix 编码后的前缀,默认"&#"
* $postfix 编码后的后缀,默认";"
*/
function unicode_encode($str, $encoding = 'utf-8', $prefix = '&#', $postfix = ';') {
 //将字符串拆分
 $str = iconv("UTF-8", "gb2312", $str);
 $cind = 0;
 $arr_cont = array();
 for ($i = 0; $i < strlen($str); $i++) {
 if (strlen(substr($str, $cind, 1)) > 0) {
  if (ord(substr($str, $cind, 1)) < 0xA1) { //如果为英文则取1个字节
  array_push($arr_cont, substr($str, $cind, 1));
  $cind++;
  } else {
  array_push($arr_cont, substr($str, $cind, 2));
  $cind+=2;
  }
 }
 }
 foreach ($arr_cont as &$row) {
 $row = iconv("gb2312", "UTF-8", $row);
 }
 //转换Unicode码
 foreach ($arr_cont as $key => $value) {
 $unicodestr.= $prefix . base_convert(bin2hex(iconv('utf-8', 'UCS-4', $value)), 16, 10) .$postfix;
 }
 return $unicodestr;
}
/**
* $str Unicode编码后的字符串
* $decoding 原始字符串的编码,默认utf-8
* $prefix 编码字符串的前缀,默认"&#"
* $postfix 编码字符串的后缀,默认";"
*/
function unicode_decode($unistr, $encoding = 'utf-8', $prefix = '&#', $postfix = ';') {
 $arruni = explode($prefix, $unistr);
 $unistr = '';
 for ($i = 1, $len = count($arruni); $i < $len; $i++) {
 if (strlen($postfix) > 0) {
  $arruni[$i] = substr($arruni[$i], 0, strlen($arruni[$i]) - strlen($postfix));
 }
 $temp = intval($arruni[$i]);
 $unistr .= ($temp < 256) ? chr(0) . chr($temp) : chr($temp / 256) . chr($temp % 256);
 }
 return iconv('UCS-2', $encoding, $unistr);
}
$str = "PHP编程:www..net";
$unistr = unicode_encode($str);
$unistr2 = unicode_decode($unistr);
echo $unistr . '<br />';
echo $unistr2 . '<br />';
$unistr = unicode_encode($str,'GBK','\\u');
$unistr2 = unicode_decode($unistr,'GBK','\\u');
echo $unistr . '<br />';
echo $unistr2 . '<br />';

PS:下面测试过这个函数比较好用,该代码需要在utf-8编码环境下运行

function unicode_encode($name) {//Unicode编码
 $jsonarr = array($name);
 $jsonstr = json_encode($jsonarr);
 if (empty ($jsonstr))
 return '';
 return substr($jsonstr,2,-2);
}
function unicode_decode($name) {//Unicode解码

 $json = '{"str":"' . $name . '"}';
 $arr = json_decode($json, true);
 if (empty ($arr))
 return '';
 return $arr['str'];

}

$test = "\u811a\u672c\u4e4b\u5bb6";
echo "unicode解码:".unicode_decode($test)."<br/>";
echo "unicode编码:".unicode_encode('')."<br/>";

PS:这里再为大家提供几款Unicode编码转换操作相关工具供大家参考使用:

在线Unicode/中文转换工具:
http://tools..net/transcoding/unicode_chinese

Native/Unicode在线编码转换工具:
http://tools..net/transcoding/native2unicode

在线中文汉字/ASCII码/Unicode编码互相转换工具:
http://tools..net/transcoding/chinese2unicode

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP编码与转码操作技巧汇总》、《php面向对象程序设计入门教程》、《PHP数学运算技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《PHP数据结构与算法教程》、《php程序设计算法总结》、《php正则表达式用法总结》、及《php常见数据库操作技巧汇总

希望本文所述对大家PHP程序设计有所帮助。