当前位置:Gxlcms > PHP教程 > php汉字unicode编码与解码

php汉字unicode编码与解码

时间:2021-07-01 10:21:17 帮助过:20人阅读

  1. //将内容进行unicode编码,编码后的内容格式:yoka\u738b (原始:yoka王)

  2. function unicode_encode($name)
  3. {
  4. $name = iconv('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. } // (脚本学堂 bbs.it-home.org 编辑整理)

  22. // 将unicode编码后的内容进行解码,编码后的内容格式:yoka\u738b (原始:yoka王)

  23. function unicode_decode($name)
  24. {
  25. // 转换编码,将unicode编码转换成可以浏览的utf-8编码
  26. $pattern = '/([\w]+)|(\\\u([\w]{4}))/i';
  27. preg_match_all($pattern, $name, $matches);
  28. if (!empty($matches))
  29. {
  30. $name = '';
  31. for ($j = 0; $j < count($matches[0]); $j++)
  32. {
  33. $str = $matches[0][$j];
  34. if (strpos($str, '\\u') === 0)
  35. {
  36. $code = base_convert(substr($str, 2, 2), 16, 10);
  37. $code2 = base_convert(substr($str, 4), 16, 10);
  38. $c = chr($code).chr($code2);
  39. $c = iconv('ucs-2', 'utf-8', $c);
  40. $name .= $c;
  41. }
  42. else
  43. {
  44. $name .= $str;
  45. }
  46. }
  47. }
  48. return $name;
  49. }

测试:

  1. echo '

    yoka\u738b -> '.unicode_decode('yoka\u738b').'

    ';
  2. $name = 'yoka王';
  3. echo '

    '.unicode_encode($name).'

    ';

注意:新浪博客的编辑器把/ ** * /全都给过滤了

人气教程排行