PageRenderTime 42ms CodeModel.GetById 4ms RepoModel.GetById 0ms app.codeStats 0ms

/3.0/obsolete/web_client/system/helpers/utf8.php

http://github.com/gallery/gallery3-contrib
PHP | 746 lines | 409 code | 93 blank | 244 comment | 75 complexity | d300adb66d0568a1ae2de8e69d239e5a MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, LGPL-2.1
  1. <?php defined('SYSPATH') OR die('No direct access allowed.');
  2. /**
  3. * A port of phputf8 to a unified file/class.
  4. *
  5. * This file is licensed differently from the rest of Kohana. As a port of
  6. * phputf8, which is LGPL software, this file is released under the LGPL.
  7. *
  8. * PCRE needs to be compiled with UTF-8 support (--enable-utf8).
  9. * Support for Unicode properties is highly recommended (--enable-unicode-properties).
  10. * @see http://php.net/manual/reference.pcre.pattern.modifiers.php
  11. *
  12. * string functions.
  13. * @see http://php.net/mbstring
  14. *
  15. * $Id$
  16. *
  17. * @package Core
  18. * @author Kohana Team
  19. * @copyright (c) 2007-2009 Kohana Team
  20. * @copyright (c) 2005 Harry Fuecks
  21. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
  22. */
  23. class utf8 {
  24. /**
  25. * Replaces text within a portion of a UTF-8 string.
  26. * @see http://php.net/substr_replace
  27. *
  28. * @author Harry Fuecks <hfuecks@gmail.com>
  29. *
  30. * @param string input string
  31. * @param string replacement string
  32. * @param integer offset
  33. * @return string
  34. */
  35. public static function substr_replace($str, $replacement, $offset, $length = NULL)
  36. {
  37. if (text::is_ascii($str))
  38. return ($length === NULL) ? substr_replace($str, $replacement, $offset) : substr_replace($str, $replacement, $offset, $length);
  39. $length = ($length === NULL) ? mb_strlen($str) : (int) $length;
  40. preg_match_all('/./us', $str, $str_array);
  41. preg_match_all('/./us', $replacement, $replacement_array);
  42. array_splice($str_array[0], $offset, $length, $replacement_array[0]);
  43. return implode('', $str_array[0]);
  44. }
  45. /**
  46. * Makes a UTF-8 string's first character uppercase.
  47. * @see http://php.net/ucfirst
  48. *
  49. * @author Harry Fuecks <hfuecks@gmail.com>
  50. *
  51. * @param string mixed case string
  52. * @return string
  53. */
  54. public static function ucfirst($str)
  55. {
  56. if (text::is_ascii($str))
  57. return ucfirst($str);
  58. preg_match('/^(.?)(.*)$/us', $str, $matches);
  59. return mb_strtoupper($matches[1]).$matches[2];
  60. }
  61. /**
  62. * Case-insensitive UTF-8 string comparison.
  63. * @see http://php.net/strcasecmp
  64. *
  65. * @author Harry Fuecks <hfuecks@gmail.com>
  66. *
  67. * @param string string to compare
  68. * @param string string to compare
  69. * @return integer less than 0 if str1 is less than str2
  70. * @return integer greater than 0 if str1 is greater than str2
  71. * @return integer 0 if they are equal
  72. */
  73. public static function strcasecmp($str1, $str2)
  74. {
  75. if (text::is_ascii($str1) AND text::is_ascii($str2))
  76. return strcasecmp($str1, $str2);
  77. $str1 = mb_strtolower($str1);
  78. $str2 = mb_strtolower($str2);
  79. return strcmp($str1, $str2);
  80. }
  81. /**
  82. * Returns a string or an array with all occurrences of search in subject (ignoring case).
  83. * replaced with the given replace value.
  84. * @see http://php.net/str_ireplace
  85. *
  86. * @note It's not fast and gets slower if $search and/or $replace are arrays.
  87. * @author Harry Fuecks <hfuecks@gmail.com
  88. *
  89. * @param string|array text to replace
  90. * @param string|array replacement text
  91. * @param string|array subject text
  92. * @param integer number of matched and replaced needles will be returned via this parameter which is passed by reference
  93. * @return string if the input was a string
  94. * @return array if the input was an array
  95. */
  96. public static function str_ireplace($search, $replace, $str, & $count = NULL)
  97. {
  98. if (text::is_ascii($search) AND text::is_ascii($replace) AND text::is_ascii($str))
  99. return str_ireplace($search, $replace, $str, $count);
  100. if (is_array($str))
  101. {
  102. foreach ($str as $key => $val)
  103. {
  104. $str[$key] = utf8::str_ireplace($search, $replace, $val, $count);
  105. }
  106. return $str;
  107. }
  108. if (is_array($search))
  109. {
  110. $keys = array_keys($search);
  111. foreach ($keys as $k)
  112. {
  113. if (is_array($replace))
  114. {
  115. if (array_key_exists($k, $replace))
  116. {
  117. $str = utf8::str_ireplace($search[$k], $replace[$k], $str, $count);
  118. }
  119. else
  120. {
  121. $str = utf8::str_ireplace($search[$k], '', $str, $count);
  122. }
  123. }
  124. else
  125. {
  126. $str = utf8::str_ireplace($search[$k], $replace, $str, $count);
  127. }
  128. }
  129. return $str;
  130. }
  131. $search = mb_strtolower($search);
  132. $str_lower = mb_strtolower($str);
  133. $total_matched_strlen = 0;
  134. $i = 0;
  135. while (preg_match('/(.*?)'.preg_quote($search, '/').'/s', $str_lower, $matches))
  136. {
  137. $matched_strlen = strlen($matches[0]);
  138. $str_lower = substr($str_lower, $matched_strlen);
  139. $offset = $total_matched_strlen + strlen($matches[1]) + ($i * (strlen($replace) - 1));
  140. $str = substr_replace($str, $replace, $offset, strlen($search));
  141. $total_matched_strlen += $matched_strlen;
  142. $i++;
  143. }
  144. $count += $i;
  145. return $str;
  146. }
  147. /**
  148. * Case-insenstive UTF-8 version of strstr. Returns all of input string
  149. * from the first occurrence of needle to the end.
  150. * @see http://php.net/stristr
  151. *
  152. * @author Harry Fuecks <hfuecks@gmail.com>
  153. *
  154. * @param string input string
  155. * @param string needle
  156. * @return string matched substring if found
  157. * @return boolean FALSE if the substring was not found
  158. */
  159. public static function stristr($str, $search)
  160. {
  161. if (text::is_ascii($str) AND text::is_ascii($search))
  162. return stristr($str, $search);
  163. if ($search == '')
  164. return $str;
  165. $str_lower = mb_strtolower($str);
  166. $search_lower = mb_strtolower($search);
  167. preg_match('/^(.*?)'.preg_quote($search, '/').'/s', $str_lower, $matches);
  168. if (isset($matches[1]))
  169. return substr($str, strlen($matches[1]));
  170. return FALSE;
  171. }
  172. /**
  173. * Finds the length of the initial segment matching mask.
  174. * @see http://php.net/strspn
  175. *
  176. * @author Harry Fuecks <hfuecks@gmail.com>
  177. *
  178. * @param string input string
  179. * @param string mask for search
  180. * @param integer start position of the string to examine
  181. * @param integer length of the string to examine
  182. * @return integer length of the initial segment that contains characters in the mask
  183. */
  184. public static function strspn($str, $mask, $offset = NULL, $length = NULL)
  185. {
  186. if ($str == '' OR $mask == '')
  187. return 0;
  188. if (text::is_ascii($str) AND text::is_ascii($mask))
  189. return ($offset === NULL) ? strspn($str, $mask) : (($length === NULL) ? strspn($str, $mask, $offset) : strspn($str, $mask, $offset, $length));
  190. if ($offset !== NULL OR $length !== NULL)
  191. {
  192. $str = mb_substr($str, $offset, $length);
  193. }
  194. // Escape these characters: - [ ] . : \ ^ /
  195. // The . and : are escaped to prevent possible warnings about POSIX regex elements
  196. $mask = preg_replace('#[-[\].:\\\\^/]#', '\\\\$0', $mask);
  197. preg_match('/^[^'.$mask.']+/u', $str, $matches);
  198. return isset($matches[0]) ? mb_strlen($matches[0]) : 0;
  199. }
  200. /**
  201. * Finds the length of the initial segment not matching mask.
  202. * @see http://php.net/strcspn
  203. *
  204. * @author Harry Fuecks <hfuecks@gmail.com>
  205. *
  206. * @param string input string
  207. * @param string mask for search
  208. * @param integer start position of the string to examine
  209. * @param integer length of the string to examine
  210. * @return integer length of the initial segment that contains characters not in the mask
  211. */
  212. public static function strcspn($str, $mask, $offset = NULL, $length = NULL)
  213. {
  214. if ($str == '' OR $mask == '')
  215. return 0;
  216. if (text::is_ascii($str) AND text::is_ascii($mask))
  217. return ($offset === NULL) ? strcspn($str, $mask) : (($length === NULL) ? strcspn($str, $mask, $offset) : strcspn($str, $mask, $offset, $length));
  218. if ($str !== NULL OR $length !== NULL)
  219. {
  220. $str = mb_substr($str, $offset, $length);
  221. }
  222. // Escape these characters: - [ ] . : \ ^ /
  223. // The . and : are escaped to prevent possible warnings about POSIX regex elements
  224. $mask = preg_replace('#[-[\].:\\\\^/]#', '\\\\$0', $mask);
  225. preg_match('/^[^'.$mask.']+/u', $str, $matches);
  226. return isset($matches[0]) ? mb_strlen($matches[0]) : 0;
  227. }
  228. /**
  229. * Pads a UTF-8 string to a certain length with another string.
  230. * @see http://php.net/str_pad
  231. *
  232. * @author Harry Fuecks <hfuecks@gmail.com>
  233. *
  234. * @param string input string
  235. * @param integer desired string length after padding
  236. * @param string string to use as padding
  237. * @param string padding type: STR_PAD_RIGHT, STR_PAD_LEFT, or STR_PAD_BOTH
  238. * @return string
  239. */
  240. public static function str_pad($str, $final_str_length, $pad_str = ' ', $pad_type = STR_PAD_RIGHT)
  241. {
  242. if (text::is_ascii($str) AND text::is_ascii($pad_str))
  243. {
  244. return str_pad($str, $final_str_length, $pad_str, $pad_type);
  245. }
  246. $str_length = mb_strlen($str);
  247. if ($final_str_length <= 0 OR $final_str_length <= $str_length)
  248. {
  249. return $str;
  250. }
  251. $pad_str_length = mb_strlen($pad_str);
  252. $pad_length = $final_str_length - $str_length;
  253. if ($pad_type == STR_PAD_RIGHT)
  254. {
  255. $repeat = ceil($pad_length / $pad_str_length);
  256. return mb_substr($str.str_repeat($pad_str, $repeat), 0, $final_str_length);
  257. }
  258. if ($pad_type == STR_PAD_LEFT)
  259. {
  260. $repeat = ceil($pad_length / $pad_str_length);
  261. return mb_substr(str_repeat($pad_str, $repeat), 0, floor($pad_length)).$str;
  262. }
  263. if ($pad_type == STR_PAD_BOTH)
  264. {
  265. $pad_length /= 2;
  266. $pad_length_left = floor($pad_length);
  267. $pad_length_right = ceil($pad_length);
  268. $repeat_left = ceil($pad_length_left / $pad_str_length);
  269. $repeat_right = ceil($pad_length_right / $pad_str_length);
  270. $pad_left = mb_substr(str_repeat($pad_str, $repeat_left), 0, $pad_length_left);
  271. $pad_right = mb_substr(str_repeat($pad_str, $repeat_right), 0, $pad_length_left);
  272. return $pad_left.$str.$pad_right;
  273. }
  274. trigger_error('utf8::str_pad: Unknown padding type (' . $pad_type . ')', E_USER_ERROR);
  275. }
  276. /**
  277. * Converts a UTF-8 string to an array.
  278. * @see http://php.net/str_split
  279. *
  280. * @author Harry Fuecks <hfuecks@gmail.com>
  281. *
  282. * @param string input string
  283. * @param integer maximum length of each chunk
  284. * @return array
  285. */
  286. public static function str_split($str, $split_length = 1)
  287. {
  288. $split_length = (int) $split_length;
  289. if (text::is_ascii($str))
  290. {
  291. return str_split($str, $split_length);
  292. }
  293. if ($split_length < 1)
  294. {
  295. return FALSE;
  296. }
  297. if (mb_strlen($str) <= $split_length)
  298. {
  299. return array($str);
  300. }
  301. preg_match_all('/.{'.$split_length.'}|[^\x00]{1,'.$split_length.'}$/us', $str, $matches);
  302. return $matches[0];
  303. }
  304. /**
  305. * Reverses a UTF-8 string.
  306. * @see http://php.net/strrev
  307. *
  308. * @author Harry Fuecks <hfuecks@gmail.com>
  309. *
  310. * @param string string to be reversed
  311. * @return string
  312. */
  313. public static function strrev($str)
  314. {
  315. if (text::is_ascii($str))
  316. return strrev($str);
  317. preg_match_all('/./us', $str, $matches);
  318. return implode('', array_reverse($matches[0]));
  319. }
  320. /**
  321. * Strips whitespace (or other UTF-8 characters) from the beginning and
  322. * end of a string.
  323. * @see http://php.net/trim
  324. *
  325. * @author Andreas Gohr <andi@splitbrain.org>
  326. *
  327. * @param string input string
  328. * @param string string of characters to remove
  329. * @return string
  330. */
  331. public static function trim($str, $charlist = NULL)
  332. {
  333. if ($charlist === NULL)
  334. return trim($str);
  335. return utf8::ltrim(utf8::rtrim($str, $charlist), $charlist);
  336. }
  337. /**
  338. * Strips whitespace (or other UTF-8 characters) from the beginning of a string.
  339. * @see http://php.net/ltrim
  340. *
  341. * @author Andreas Gohr <andi@splitbrain.org>
  342. *
  343. * @param string input string
  344. * @param string string of characters to remove
  345. * @return string
  346. */
  347. public static function ltrim($str, $charlist = NULL)
  348. {
  349. if ($charlist === NULL)
  350. return ltrim($str);
  351. if (text::is_ascii($charlist))
  352. return ltrim($str, $charlist);
  353. $charlist = preg_replace('#[-\[\]:\\\\^/]#', '\\\\$0', $charlist);
  354. return preg_replace('/^['.$charlist.']+/u', '', $str);
  355. }
  356. /**
  357. * Strips whitespace (or other UTF-8 characters) from the end of a string.
  358. * @see http://php.net/rtrim
  359. *
  360. * @author Andreas Gohr <andi@splitbrain.org>
  361. *
  362. * @param string input string
  363. * @param string string of characters to remove
  364. * @return string
  365. */
  366. public static function rtrim($str, $charlist = NULL)
  367. {
  368. if ($charlist === NULL)
  369. return rtrim($str);
  370. if (text::is_ascii($charlist))
  371. return rtrim($str, $charlist);
  372. $charlist = preg_replace('#[-\[\]:\\\\^/]#', '\\\\$0', $charlist);
  373. return preg_replace('/['.$charlist.']++$/uD', '', $str);
  374. }
  375. /**
  376. * Returns the unicode ordinal for a character.
  377. * @see http://php.net/ord
  378. *
  379. * @author Harry Fuecks <hfuecks@gmail.com>
  380. *
  381. * @param string UTF-8 encoded character
  382. * @return integer
  383. */
  384. public static function ord($chr)
  385. {
  386. $ord0 = ord($chr);
  387. if ($ord0 >= 0 AND $ord0 <= 127)
  388. {
  389. return $ord0;
  390. }
  391. if ( ! isset($chr[1]))
  392. {
  393. trigger_error('Short sequence - at least 2 bytes expected, only 1 seen', E_USER_WARNING);
  394. return FALSE;
  395. }
  396. $ord1 = ord($chr[1]);
  397. if ($ord0 >= 192 AND $ord0 <= 223)
  398. {
  399. return ($ord0 - 192) * 64 + ($ord1 - 128);
  400. }
  401. if ( ! isset($chr[2]))
  402. {
  403. trigger_error('Short sequence - at least 3 bytes expected, only 2 seen', E_USER_WARNING);
  404. return FALSE;
  405. }
  406. $ord2 = ord($chr[2]);
  407. if ($ord0 >= 224 AND $ord0 <= 239)
  408. {
  409. return ($ord0 - 224) * 4096 + ($ord1 - 128) * 64 + ($ord2 - 128);
  410. }
  411. if ( ! isset($chr[3]))
  412. {
  413. trigger_error('Short sequence - at least 4 bytes expected, only 3 seen', E_USER_WARNING);
  414. return FALSE;
  415. }
  416. $ord3 = ord($chr[3]);
  417. if ($ord0 >= 240 AND $ord0 <= 247)
  418. {
  419. return ($ord0 - 240) * 262144 + ($ord1 - 128) * 4096 + ($ord2-128) * 64 + ($ord3 - 128);
  420. }
  421. if ( ! isset($chr[4]))
  422. {
  423. trigger_error('Short sequence - at least 5 bytes expected, only 4 seen', E_USER_WARNING);
  424. return FALSE;
  425. }
  426. $ord4 = ord($chr[4]);
  427. if ($ord0 >= 248 AND $ord0 <= 251)
  428. {
  429. return ($ord0 - 248) * 16777216 + ($ord1-128) * 262144 + ($ord2 - 128) * 4096 + ($ord3 - 128) * 64 + ($ord4 - 128);
  430. }
  431. if ( ! isset($chr[5]))
  432. {
  433. trigger_error('Short sequence - at least 6 bytes expected, only 5 seen', E_USER_WARNING);
  434. return FALSE;
  435. }
  436. if ($ord0 >= 252 AND $ord0 <= 253)
  437. {
  438. return ($ord0 - 252) * 1073741824 + ($ord1 - 128) * 16777216 + ($ord2 - 128) * 262144 + ($ord3 - 128) * 4096 + ($ord4 - 128) * 64 + (ord($chr[5]) - 128);
  439. }
  440. if ($ord0 >= 254 AND $ord0 <= 255)
  441. {
  442. trigger_error('Invalid UTF-8 with surrogate ordinal '.$ord0, E_USER_WARNING);
  443. return FALSE;
  444. }
  445. }
  446. /**
  447. * Takes an UTF-8 string and returns an array of ints representing the Unicode characters.
  448. * Astral planes are supported i.e. the ints in the output can be > 0xFFFF.
  449. * Occurrances of the BOM are ignored. Surrogates are not allowed.
  450. *
  451. * The Original Code is Mozilla Communicator client code.
  452. * The Initial Developer of the Original Code is Netscape Communications Corporation.
  453. * Portions created by the Initial Developer are Copyright (C) 1998 the Initial Developer.
  454. * Ported to PHP by Henri Sivonen <hsivonen@iki.fi>, see http://hsivonen.iki.fi/php-utf8/.
  455. * Slight modifications to fit with phputf8 library by Harry Fuecks <hfuecks@gmail.com>.
  456. *
  457. * @param string UTF-8 encoded string
  458. * @return array unicode code points
  459. * @return boolean FALSE if the string is invalid
  460. */
  461. public static function to_unicode($str)
  462. {
  463. $mState = 0; // cached expected number of octets after the current octet until the beginning of the next UTF8 character sequence
  464. $mUcs4 = 0; // cached Unicode character
  465. $mBytes = 1; // cached expected number of octets in the current sequence
  466. $out = array();
  467. $len = strlen($str);
  468. for ($i = 0; $i < $len; $i++)
  469. {
  470. $in = ord($str[$i]);
  471. if ($mState == 0)
  472. {
  473. // When mState is zero we expect either a US-ASCII character or a
  474. // multi-octet sequence.
  475. if (0 == (0x80 & $in))
  476. {
  477. // US-ASCII, pass straight through.
  478. $out[] = $in;
  479. $mBytes = 1;
  480. }
  481. elseif (0xC0 == (0xE0 & $in))
  482. {
  483. // First octet of 2 octet sequence
  484. $mUcs4 = $in;
  485. $mUcs4 = ($mUcs4 & 0x1F) << 6;
  486. $mState = 1;
  487. $mBytes = 2;
  488. }
  489. elseif (0xE0 == (0xF0 & $in))
  490. {
  491. // First octet of 3 octet sequence
  492. $mUcs4 = $in;
  493. $mUcs4 = ($mUcs4 & 0x0F) << 12;
  494. $mState = 2;
  495. $mBytes = 3;
  496. }
  497. elseif (0xF0 == (0xF8 & $in))
  498. {
  499. // First octet of 4 octet sequence
  500. $mUcs4 = $in;
  501. $mUcs4 = ($mUcs4 & 0x07) << 18;
  502. $mState = 3;
  503. $mBytes = 4;
  504. }
  505. elseif (0xF8 == (0xFC & $in))
  506. {
  507. // First octet of 5 octet sequence.
  508. //
  509. // This is illegal because the encoded codepoint must be either
  510. // (a) not the shortest form or
  511. // (b) outside the Unicode range of 0-0x10FFFF.
  512. // Rather than trying to resynchronize, we will carry on until the end
  513. // of the sequence and let the later error handling code catch it.
  514. $mUcs4 = $in;
  515. $mUcs4 = ($mUcs4 & 0x03) << 24;
  516. $mState = 4;
  517. $mBytes = 5;
  518. }
  519. elseif (0xFC == (0xFE & $in))
  520. {
  521. // First octet of 6 octet sequence, see comments for 5 octet sequence.
  522. $mUcs4 = $in;
  523. $mUcs4 = ($mUcs4 & 1) << 30;
  524. $mState = 5;
  525. $mBytes = 6;
  526. }
  527. else
  528. {
  529. // Current octet is neither in the US-ASCII range nor a legal first octet of a multi-octet sequence.
  530. trigger_error('utf8::to_unicode: Illegal sequence identifier in UTF-8 at byte '.$i, E_USER_WARNING);
  531. return FALSE;
  532. }
  533. }
  534. else
  535. {
  536. // When mState is non-zero, we expect a continuation of the multi-octet sequence
  537. if (0x80 == (0xC0 & $in))
  538. {
  539. // Legal continuation
  540. $shift = ($mState - 1) * 6;
  541. $tmp = $in;
  542. $tmp = ($tmp & 0x0000003F) << $shift;
  543. $mUcs4 |= $tmp;
  544. // End of the multi-octet sequence. mUcs4 now contains the final Unicode codepoint to be output
  545. if (0 == --$mState)
  546. {
  547. // Check for illegal sequences and codepoints
  548. // From Unicode 3.1, non-shortest form is illegal
  549. if (((2 == $mBytes) AND ($mUcs4 < 0x0080)) OR
  550. ((3 == $mBytes) AND ($mUcs4 < 0x0800)) OR
  551. ((4 == $mBytes) AND ($mUcs4 < 0x10000)) OR
  552. (4 < $mBytes) OR
  553. // From Unicode 3.2, surrogate characters are illegal
  554. (($mUcs4 & 0xFFFFF800) == 0xD800) OR
  555. // Codepoints outside the Unicode range are illegal
  556. ($mUcs4 > 0x10FFFF))
  557. {
  558. trigger_error('utf8::to_unicode: Illegal sequence or codepoint in UTF-8 at byte '.$i, E_USER_WARNING);
  559. return FALSE;
  560. }
  561. if (0xFEFF != $mUcs4)
  562. {
  563. // BOM is legal but we don't want to output it
  564. $out[] = $mUcs4;
  565. }
  566. // Initialize UTF-8 cache
  567. $mState = 0;
  568. $mUcs4 = 0;
  569. $mBytes = 1;
  570. }
  571. }
  572. else
  573. {
  574. // ((0xC0 & (*in) != 0x80) AND (mState != 0))
  575. // Incomplete multi-octet sequence
  576. trigger_error('utf8::to_unicode: Incomplete multi-octet sequence in UTF-8 at byte '.$i, E_USER_WARNING);
  577. return FALSE;
  578. }
  579. }
  580. }
  581. return $out;
  582. }
  583. /**
  584. * Takes an array of ints representing the Unicode characters and returns a UTF-8 string.
  585. * Astral planes are supported i.e. the ints in the input can be > 0xFFFF.
  586. * Occurrances of the BOM are ignored. Surrogates are not allowed.
  587. *
  588. * The Original Code is Mozilla Communicator client code.
  589. * The Initial Developer of the Original Code is Netscape Communications Corporation.
  590. * Portions created by the Initial Developer are Copyright (C) 1998 the Initial Developer.
  591. * Ported to PHP by Henri Sivonen <hsivonen@iki.fi>, see http://hsivonen.iki.fi/php-utf8/.
  592. * Slight modifications to fit with phputf8 library by Harry Fuecks <hfuecks@gmail.com>.
  593. *
  594. * @param array unicode code points representing a string
  595. * @return string utf8 string of characters
  596. * @return boolean FALSE if a code point cannot be found
  597. */
  598. public static function from_unicode($arr)
  599. {
  600. ob_start();
  601. $keys = array_keys($arr);
  602. foreach ($keys as $k)
  603. {
  604. // ASCII range (including control chars)
  605. if (($arr[$k] >= 0) AND ($arr[$k] <= 0x007f))
  606. {
  607. echo chr($arr[$k]);
  608. }
  609. // 2 byte sequence
  610. elseif ($arr[$k] <= 0x07ff)
  611. {
  612. echo chr(0xc0 | ($arr[$k] >> 6));
  613. echo chr(0x80 | ($arr[$k] & 0x003f));
  614. }
  615. // Byte order mark (skip)
  616. elseif ($arr[$k] == 0xFEFF)
  617. {
  618. // nop -- zap the BOM
  619. }
  620. // Test for illegal surrogates
  621. elseif ($arr[$k] >= 0xD800 AND $arr[$k] <= 0xDFFF)
  622. {
  623. // Found a surrogate
  624. trigger_error('utf8::from_unicode: Illegal surrogate at index: '.$k.', value: '.$arr[$k], E_USER_WARNING);
  625. return FALSE;
  626. }
  627. // 3 byte sequence
  628. elseif ($arr[$k] <= 0xffff)
  629. {
  630. echo chr(0xe0 | ($arr[$k] >> 12));
  631. echo chr(0x80 | (($arr[$k] >> 6) & 0x003f));
  632. echo chr(0x80 | ($arr[$k] & 0x003f));
  633. }
  634. // 4 byte sequence
  635. elseif ($arr[$k] <= 0x10ffff)
  636. {
  637. echo chr(0xf0 | ($arr[$k] >> 18));
  638. echo chr(0x80 | (($arr[$k] >> 12) & 0x3f));
  639. echo chr(0x80 | (($arr[$k] >> 6) & 0x3f));
  640. echo chr(0x80 | ($arr[$k] & 0x3f));
  641. }
  642. // Out of range
  643. else
  644. {
  645. trigger_error('utf8::from_unicode: Codepoint out of Unicode range at index: '.$k.', value: '.$arr[$k], E_USER_WARNING);
  646. return FALSE;
  647. }
  648. }
  649. $result = ob_get_contents();
  650. ob_end_clean();
  651. return $result;
  652. }
  653. } // End utf8