PageRenderTime 36ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/include/utf8/utils/unicode.php

https://bitbucket.org/gencer/fluxbb
PHP | 241 lines | 138 code | 16 blank | 87 comment | 50 complexity | 11e7903669f99e4056f32bc68077c47a MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * @version $Id: unicode.php,v 1.2 2006/02/26 13:20:44 harryf Exp $
  4. * Tools for conversion between UTF-8 and unicode
  5. * The Original Code is Mozilla Communicator client code.
  6. * The Initial Developer of the Original Code is
  7. * Netscape Communications Corporation.
  8. * Portions created by the Initial Developer are Copyright (C) 1998
  9. * the Initial Developer. All Rights Reserved.
  10. * Ported to PHP by Henri Sivonen (http://hsivonen.iki.fi)
  11. * Slight modifications to fit with phputf8 library by Harry Fuecks (hfuecks gmail com)
  12. * @see http://lxr.mozilla.org/seamonkey/source/intl/uconv/src/nsUTF8ToUnicode.cpp
  13. * @see http://lxr.mozilla.org/seamonkey/source/intl/uconv/src/nsUnicodeToUTF8.cpp
  14. * @see http://hsivonen.iki.fi/php-utf8/
  15. * @package utf8
  16. * @subpackage unicode
  17. */
  18. /**
  19. * Takes an UTF-8 string and returns an array of ints representing the
  20. * Unicode characters. Astral planes are supported ie. the ints in the
  21. * output can be > 0xFFFF. Occurrances of the BOM are ignored. Surrogates
  22. * are not allowed.
  23. * Returns false if the input string isn't a valid UTF-8 octet sequence
  24. * and raises a PHP error at level E_USER_WARNING
  25. * Note: this function has been modified slightly in this library to
  26. * trigger errors on encountering bad bytes
  27. * @author <hsivonen@iki.fi>
  28. * @param string UTF-8 encoded string
  29. * @return mixed array of unicode code points or FALSE if UTF-8 invalid
  30. * @see utf8_from_unicode
  31. * @see http://hsivonen.iki.fi/php-utf8/
  32. * @package utf8
  33. * @subpackage unicode
  34. */
  35. function utf8_to_unicode($str)
  36. {
  37. $mState = 0; // Cached expected number of octets after the current octet
  38. // until the beginning of the next UTF8 character sequence
  39. $mUcs4 = 0; // Cached Unicode character
  40. $mBytes = 1; // Cached expected number of octets in the current sequence
  41. $out = array();
  42. $len = strlen($str);
  43. for($i = 0; $i < $len; $i++)
  44. {
  45. $in = ord($str[$i]);
  46. if ($mState == 0)
  47. {
  48. // When mState is zero we expect either a US-ASCII character or a multi-octet sequence.
  49. if (0 == (0x80 & ($in)))
  50. {
  51. // US-ASCII, pass straight through.
  52. $out[] = $in;
  53. $mBytes = 1;
  54. }
  55. else if (0xC0 == (0xE0 & ($in)))
  56. {
  57. // First octet of 2 octet sequence
  58. $mUcs4 = ($in);
  59. $mUcs4 = ($mUcs4 & 0x1F) << 6;
  60. $mState = 1;
  61. $mBytes = 2;
  62. }
  63. else if (0xE0 == (0xF0 & ($in)))
  64. {
  65. // First octet of 3 octet sequence
  66. $mUcs4 = ($in);
  67. $mUcs4 = ($mUcs4 & 0x0F) << 12;
  68. $mState = 2;
  69. $mBytes = 3;
  70. }
  71. else if (0xF0 == (0xF8 & ($in)))
  72. {
  73. // First octet of 4 octet sequence
  74. $mUcs4 = ($in);
  75. $mUcs4 = ($mUcs4 & 0x07) << 18;
  76. $mState = 3;
  77. $mBytes = 4;
  78. }
  79. else if (0xF8 == (0xFC & ($in)))
  80. {
  81. /* First octet of 5 octet sequence.
  82. *
  83. * This is illegal because the encoded codepoint must be either
  84. * (a) not the shortest form or
  85. * (b) outside the Unicode range of 0-0x10FFFF.
  86. * Rather than trying to resynchronize, we will carry on until the end
  87. * of the sequence and let the later error handling code catch it.
  88. */
  89. $mUcs4 = ($in);
  90. $mUcs4 = ($mUcs4 & 0x03) << 24;
  91. $mState = 4;
  92. $mBytes = 5;
  93. }
  94. else if (0xFC == (0xFE & ($in)))
  95. {
  96. // First octet of 6 octet sequence, see comments for 5 octet sequence.
  97. $mUcs4 = ($in);
  98. $mUcs4 = ($mUcs4 & 1) << 30;
  99. $mState = 5;
  100. $mBytes = 6;
  101. }
  102. else
  103. {
  104. // Current octet is neither in the US-ASCII range nor a legal first octet of a multi-octet sequence
  105. trigger_error('utf8_to_unicode: Illegal sequence identifier in UTF-8 at byte '.$i, E_USER_WARNING);
  106. return false;
  107. }
  108. }
  109. else
  110. {
  111. // When mState is non-zero, we expect a continuation of the multi-octet sequence
  112. if (0x80 == (0xC0 & ($in)))
  113. {
  114. // Legal continuation.
  115. $shift = ($mState - 1) * 6;
  116. $tmp = $in;
  117. $tmp = ($tmp & 0x0000003F) << $shift;
  118. $mUcs4 |= $tmp;
  119. /**
  120. * End of the multi-octet sequence. mUcs4 now contains the final
  121. * Unicode codepoint to be output
  122. */
  123. if (0 == --$mState)
  124. {
  125. /*
  126. * Check for illegal sequences and codepoints.
  127. */
  128. // From Unicode 3.1, non-shortest form is illegal
  129. if (((2 == $mBytes) && ($mUcs4 < 0x0080)) || ((3 == $mBytes) && ($mUcs4 < 0x0800)) ||
  130. ((4 == $mBytes) && ($mUcs4 < 0x10000)) || (4 < $mBytes) ||
  131. // From Unicode 3.2, surrogate characters are illegal
  132. (($mUcs4 & 0xFFFFF800) == 0xD800) ||
  133. // Codepoints outside the Unicode range are illegal
  134. ($mUcs4 > 0x10FFFF))
  135. {
  136. trigger_error('utf8_to_unicode: Illegal sequence or codepoint in UTF-8 at byte '.$i, E_USER_WARNING);
  137. return false;
  138. }
  139. // BOM is legal but we don't want to output it
  140. if (0xFEFF != $mUcs4)
  141. $out[] = $mUcs4;
  142. // Initialize UTF8 cache
  143. $mState = 0;
  144. $mUcs4 = 0;
  145. $mBytes = 1;
  146. }
  147. }
  148. else
  149. {
  150. /* ((0xC0 & (*in) != 0x80) && (mState != 0))
  151. Incomplete multi-octet sequence. */
  152. trigger_error('utf8_to_unicode: Incomplete multi-octet sequence in UTF-8 at byte '.$i, E_USER_WARNING);
  153. return false;
  154. }
  155. }
  156. }
  157. return $out;
  158. }
  159. /**
  160. * Takes an array of ints representing the Unicode characters and returns
  161. * a UTF-8 string. Astral planes are supported ie. the ints in the
  162. * input can be > 0xFFFF. Occurrances of the BOM are ignored. Surrogates
  163. * are not allowed.
  164. * Returns false if the input array contains ints that represent
  165. * surrogates or are outside the Unicode range
  166. * and raises a PHP error at level E_USER_WARNING
  167. * Note: this function has been modified slightly in this library to use
  168. * output buffering to concatenate the UTF-8 string (faster) as well as
  169. * reference the array by it's keys
  170. * @param array of unicode code points representing a string
  171. * @return mixed UTF-8 string or FALSE if array contains invalid code points
  172. * @author <hsivonen@iki.fi>
  173. * @see utf8_to_unicode
  174. * @see http://hsivonen.iki.fi/php-utf8/
  175. * @package utf8
  176. * @subpackage unicode
  177. */
  178. function utf8_from_unicode($arr)
  179. {
  180. ob_start();
  181. foreach (array_keys($arr) as $k)
  182. {
  183. if ( ($arr[$k] >= 0) && ($arr[$k] <= 0x007f) ) // ASCII range (including control chars)
  184. {
  185. echo chr($arr[$k]);
  186. }
  187. else if ($arr[$k] <= 0x07ff) //2 byte sequence
  188. {
  189. echo chr(0xc0 | ($arr[$k] >> 6));
  190. echo chr(0x80 | ($arr[$k] & 0x003f));
  191. }
  192. else if($arr[$k] == 0xFEFF) // Byte order mark (skip)
  193. {
  194. // Nop -- zap the BOM
  195. }
  196. else if ($arr[$k] >= 0xD800 && $arr[$k] <= 0xDFFF) // Test for illegal surrogates
  197. {
  198. // Found a surrogate
  199. trigger_error('utf8_from_unicode: Illegal surrogate at index: '.$k.', value: '.$arr[$k], E_USER_WARNING);
  200. return false;
  201. }
  202. else if ($arr[$k] <= 0xffff) // 3 byte sequence
  203. {
  204. echo chr(0xe0 | ($arr[$k] >> 12));
  205. echo chr(0x80 | (($arr[$k] >> 6) & 0x003f));
  206. echo chr(0x80 | ($arr[$k] & 0x003f));
  207. }
  208. else if ($arr[$k] <= 0x10ffff) // 4 byte sequence
  209. {
  210. echo chr(0xf0 | ($arr[$k] >> 18));
  211. echo chr(0x80 | (($arr[$k] >> 12) & 0x3f));
  212. echo chr(0x80 | (($arr[$k] >> 6) & 0x3f));
  213. echo chr(0x80 | ($arr[$k] & 0x3f));
  214. }
  215. else
  216. {
  217. trigger_error('utf8_from_unicode: Codepoint out of Unicode range at index: '.$k.', value: '.$arr[$k], E_USER_WARNING);
  218. // Out of range
  219. return false;
  220. }
  221. }
  222. $result = ob_get_contents();
  223. ob_end_clean();
  224. return $result;
  225. }