首頁 >後端開發 >php教程 >php實作的一個UTF8編碼轉Unicode的函數

php實作的一個UTF8編碼轉Unicode的函數

WBOY
WBOY原創
2016-07-25 09:07:12817瀏覽
  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. ?>

复制代码


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn