PHP中对汉字进行unicode编码和解码的实现方法

  1. //将内容进行UNICODE编码
  2. functionunicode_encode($name)
  3. {
  4. $name= <a href="/tags.php/iconv/" target="_blank">iconv</a>('UTF-8','UCS-2',$name);
  5. $len=strlen($name);
  6. $str=''
  7. for($i= 0;$i<$len- 1;$i=$i+ 2)
  8. {
  9. $c=$name[$i];
  10. $c2=$name[$i+ 1];
  11. if(ord($c) > 0)
  12. { // 两个字节的文字
  13. $str.='\u'.base_convert(ord($c), 10, 16).base_convert(ord($c2), 10, 16);
  14. }
  15. else
  16. {
  17. $str.=$c2;
  18. }
  19. }
  20. return$str;
  21. }
  22. $name='MY,你大爷的'
  23. $unicode_name=unicode_encode($name);
  24. echo'
  25. '
  26. // 将UNICODE编码后的内容进行解码
  27. functionunicode_decode($name)
  28. {
  29. // 转换编码,将Unicode编码转换成可以浏览的utf-8编码
  30. $pattern='/([\w]+)|(\\\u([\w]{4}))/i'
  31. <a href="/tags.php/preg_match/" target="_blank">preg_match</a>_all($pattern,$name,$matches);
  32. if(!emptyempty($matches))
  33. {
  34. $name=''
  35. for($j= 0;$j<count($matches[0]);$j++)
  36. {
  37. $str=$matches[0][$j];
  38. if(strpos($str,'\\u') === 0)
  39. {
  40. $code=base_convert(<a href="/tags.php/substr/" target="_blank">substr</a>($str, 2, 2), 16, 10);
  41. $code2=base_convert(substr($str, 4), 16, 10);
  42. $c=chr($code).chr($code2);
  43. $c= iconv('UCS-2','UTF-8',$c);
  44. $name.=$c;
  45. }
  46. else
  47. {
  48. $name.=$str;
  49. }
  50. }
  51. }
  52. return$name;
  53. }
  54. echo'MY,\u4f60\u5927\u7237\u7684 -> '.unicode_decode($unicode_name);