Home > Backend Development > PHP Tutorial > PHP Chinese character transcoding Unicode encoding function

PHP Chinese character transcoding Unicode encoding function

WBOY
Release: 2016-07-25 08:53:48
Original
3672 people have browsed it
  1. /**
  2. * $str original string
  3. * $encoding encoding of the original string, defaults to GBK
  4. * $prefix encoded prefix, defaults to "&#"
  5. * $postfix encoded suffix, defaults to ";"
  6. */
  7. function unicode_encode($str, $encoding = 'GBK', $prefix = '&#', $postfix = ';') {
  8. $str = iconv($encoding, 'UCS-2', $str);
  9. $arrstr = str_split($str, 2);
  10. $unistr = '';
  11. for($i = 0, $len = count($arrstr); $i < $len; $i++) {
  12. $dec = hexdec(bin2hex($arrstr[$i]));
  13. $unistr .= $prefix . $dec . $postfix;
  14. }
  15. return $unistr;
  16. } // 脚本学堂 bbs.it-home.org
  17. /**
  18. * $str Unicode encoded string
  19. * $encoding The encoding of the original string, the default is GBK
  20. * $prefix The prefix of the encoded string, the default is "&#"
  21. * $postfix The suffix of the encoded string, the default is "; "
  22. */
  23. function unicode_decode($unistr, $encoding = 'GBK', $prefix = '&#', $postfix = ';') {
  24. $arruni = explode($prefix, $unistr);
  25. $unistr = '';
  26. for($i = 1, $len = count($arruni); $i < $len; $i++) {
  27. if (strlen($postfix) > 0) {
  28. $arruni[$i] = substr($arruni[$i], 0, strlen($arruni[$i]) - strlen($postfix));
  29. }
  30. $temp = intval($arruni[$i]);
  31. $unistr .= ($temp < 256) ? chr(0) . chr($temp) : chr($temp / 256) . chr($temp % 256);
  32. }
  33. return iconv('UCS-2', $encoding, $unistr);
  34. }
复制代码

例子,php汉字转换函数的范例,实现编码转换:

  1. //GBK字符串测试
  2. $str = '哈哈';
  3. echo $str.'
    ';
  4. $unistr = unicode_encode($str);
  5. echo $unistr.'
    '; // 哈哈
  6. $str2 = unicode_decode($unistr);
  7. echo $str2.'
    '; //哈哈
  8. //UTF-8字符串测试
  9. $utf8_str = iconv('GBK', 'UTF-8', $str);
  10. echo $utf8_str.'
    '; // 鍝堝搱 注:UTF在GBK下显示的乱码!可切换浏览器的编码测试
  11. $utf8_unistr = unicode_encode($utf8_str, 'UTF-8');
  12. echo $utf8_unistr.'
    '; // 哈哈
  13. $utf8_str2 = unicode_decode($utf8_unistr, 'UTF-8');
  14. echo $utf8_str2.'
    '; // 鍝堝搱
  15. //其它后缀、前缀测试
  16. $prefix_unistr = unicode_encode($str, 'GBK', "\u", '');
  17. echo $prefix_unistr.'
    '; // u60u98u62u21704u21704u60u47u98u62
  18. $profix_unistr2 = unicode_decode($prefix_unistr, 'GBK', "\u", '');
  19. echo $profix_unistr2.'
    '; //哈哈
复制代码


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template