当前位置:Gxlcms > PHP教程 > php实现的一个UTF8编码转Unicode的函数

php实现的一个UTF8编码转Unicode的函数

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

  1. function Utf8ToUnicode(strUtf8)

  2. {
  3. var bstr = “”;
  4. var nTotalChars = strUtf8.length; // total chars to be processed.
  5. var nOffset = 0; // processing point on strUtf8
  6. var nRemainingBytes = nTotalChars; // how many bytes left to be converted
  7. var nOutputPosition = 0;
  8. var iCode, iCode1, iCode2; // the value of the unicode.

  9. while (nOffset < nTotalChars)

  10. {
  11. iCode = strUtf8.charCodeAt(nOffset);
  12. if ((iCode & 0×80) == 0) // 1 byte.
  13. {
  14. if ( nRemainingBytes < 1 ) // not enough data
  15. break;

  16. bstr += String.fromCharCode(iCode & 0×7F);

  17. nOffset ++;
  18. nRemainingBytes -= 1;
  19. }
  20. else if ((iCode & 0xE0) == 0xC0) // 2 bytes
  21. {
  22. iCode1 = strUtf8.charCodeAt(nOffset + 1);
  23. if ( nRemainingBytes < 2 || // not enough data
  24. (iCode1 & 0xC0) != 0×80 ) // invalid pattern
  25. {
  26. break;
  27. }

  28. bstr += String.fromCharCode(((iCode & 0×3F) << 6) | ( iCode1 & 0×3F));

  29. nOffset += 2;
  30. nRemainingBytes -= 2;
  31. }
  32. else if ((iCode & 0xF0) == 0xE0) // 3 bytes
  33. {
  34. iCode1 = strUtf8.charCodeAt(nOffset + 1);
  35. iCode2 = strUtf8.charCodeAt(nOffset + 2);
  36. if ( nRemainingBytes < 3 || // not enough data
  37. (iCode1 & 0xC0) != 0×80 || // invalid pattern
  38. (iCode2 & 0xC0) != 0×80 )
  39. {
  40. break;
  41. }

  42. bstr += String.fromCharCode(((iCode & 0×0F) << 12) |

  43. ((iCode1 & 0×3F) << 6) |
  44. (iCode2 & 0×3F));
  45. nOffset += 3;
  46. nRemainingBytes -= 3;
  47. }
  48. else // 4 or more bytes — unsupported
  49. break;
  50. }

  51. if (nRemainingBytes != 0)

  52. {
  53. // bad UTF8 string.
  54. return “”;
  55. }

  56. return bstr;

  57. }
  58. ?>

人气教程排行