PageRenderTime 53ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/system/helpers/utf8.php

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