Home > Backend Development > PHP Tutorial > A function that converts UTF8 encoding to Unicode implemented in PHP

A function that converts UTF8 encoding to Unicode implemented in PHP

WBOY
Release: 2016-07-25 09:07:12
Original
813 people have browsed it
  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. ?>

复制代码


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