PageRenderTime 63ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/utf8/utf8.php

http://github.com/usebb/UseBB
PHP | 1210 lines | 704 code | 119 blank | 387 comment | 166 complexity | ab9d97799002699b40d06d9f73e7e975 MD5 | raw file
Possible License(s): GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. namespace DokuWiki\UTF8;
  3. /**
  4. * UTF8 helper functions
  5. *
  6. * @license LGPL 2.1 (http://www.gnu.org/copyleft/lesser.html)
  7. * @author Andreas Gohr <andi@splitbrain.org>
  8. */
  9. /**
  10. * check for mb_string support
  11. */
  12. if(!defined('UTF8_MBSTRING')){
  13. if(function_exists('mb_substr') && !defined('UTF8_NOMBSTRING')){
  14. define('UTF8_MBSTRING',1);
  15. }else{
  16. define('UTF8_MBSTRING',0);
  17. }
  18. }
  19. if(UTF8_MBSTRING){ mb_internal_encoding('UTF-8'); }
  20. if(!function_exists('utf8_isASCII')){
  21. /**
  22. * Checks if a string contains 7bit ASCII only
  23. *
  24. * @author Andreas Haerter <andreas.haerter@dev.mail-node.com>
  25. */
  26. function utf8_isASCII($str){
  27. return (preg_match('/(?:[^\x00-\x7F])/', $str) !== 1);
  28. }
  29. }
  30. if(!function_exists('utf8_strip')){
  31. /**
  32. * Strips all highbyte chars
  33. *
  34. * Returns a pure ASCII7 string
  35. *
  36. * @author Andreas Gohr <andi@splitbrain.org>
  37. */
  38. function utf8_strip($str){
  39. $ascii = '';
  40. $len = strlen($str);
  41. for($i=0; $i<$len; $i++){
  42. if(ord($str{$i}) <128){
  43. $ascii .= $str{$i};
  44. }
  45. }
  46. return $ascii;
  47. }
  48. }
  49. if(!function_exists('utf8_check')){
  50. /**
  51. * Tries to detect if a string is in Unicode encoding
  52. *
  53. * @author <bmorel@ssi.fr>
  54. * @link http://www.php.net/manual/en/function.utf8-encode.php
  55. */
  56. function utf8_check($Str) {
  57. $len = strlen($Str);
  58. for ($i=0; $i<$len; $i++) {
  59. $b = ord($Str[$i]);
  60. if ($b < 0x80) continue; # 0bbbbbbb
  61. elseif (($b & 0xE0) == 0xC0) $n=1; # 110bbbbb
  62. elseif (($b & 0xF0) == 0xE0) $n=2; # 1110bbbb
  63. elseif (($b & 0xF8) == 0xF0) $n=3; # 11110bbb
  64. elseif (($b & 0xFC) == 0xF8) $n=4; # 111110bb
  65. elseif (($b & 0xFE) == 0xFC) $n=5; # 1111110b
  66. else return false; # Does not match any model
  67. for ($j=0; $j<$n; $j++) { # n bytes matching 10bbbbbb follow ?
  68. if ((++$i == $len) || ((ord($Str[$i]) & 0xC0) != 0x80))
  69. return false;
  70. }
  71. }
  72. return true;
  73. }
  74. }
  75. if(!function_exists('utf8_strlen')){
  76. /**
  77. * Unicode aware replacement for strlen()
  78. *
  79. * utf8_decode() converts characters that are not in ISO-8859-1
  80. * to '?', which, for the purpose of counting, is alright - It's
  81. * even faster than mb_strlen.
  82. *
  83. * @author <chernyshevsky at hotmail dot com>
  84. * @see strlen()
  85. * @see utf8_decode()
  86. */
  87. function utf8_strlen($string){
  88. return strlen(utf8_decode($string));
  89. }
  90. }
  91. if(!function_exists('utf8_substr')){
  92. /**
  93. * UTF-8 aware alternative to substr
  94. *
  95. * Return part of a string given character offset (and optionally length)
  96. *
  97. * @author Harry Fuecks <hfuecks@gmail.com>
  98. * @author Chris Smith <chris@jalakai.co.uk>
  99. * @param string
  100. * @param integer number of UTF-8 characters offset (from left)
  101. * @param integer (optional) length in UTF-8 characters from offset
  102. * @return mixed string or false if failure
  103. */
  104. function utf8_substr($str, $offset, $length = null) {
  105. if(UTF8_MBSTRING){
  106. if( $length === null ){
  107. return mb_substr($str, $offset);
  108. }else{
  109. return mb_substr($str, $offset, $length);
  110. }
  111. }
  112. /*
  113. * Notes:
  114. *
  115. * no mb string support, so we'll use pcre regex's with 'u' flag
  116. * pcre only supports repetitions of less than 65536, in order to accept up to MAXINT values for
  117. * offset and length, we'll repeat a group of 65535 characters when needed (ok, up to MAXINT-65536)
  118. *
  119. * substr documentation states false can be returned in some cases (e.g. offset > string length)
  120. * mb_substr never returns false, it will return an empty string instead.
  121. *
  122. * calculating the number of characters in the string is a relatively expensive operation, so
  123. * we only carry it out when necessary. It isn't necessary for +ve offsets and no specified length
  124. */
  125. // cast parameters to appropriate types to avoid multiple notices/warnings
  126. $str = (string)$str; // generates E_NOTICE for PHP4 objects, but not PHP5 objects
  127. $offset = (int)$offset;
  128. if (!is_null($length)) $length = (int)$length;
  129. // handle trivial cases
  130. if ($length === 0) return '';
  131. if ($offset < 0 && $length < 0 && $length < $offset) return '';
  132. $offset_pattern = '';
  133. $length_pattern = '';
  134. // normalise -ve offsets (we could use a tail anchored pattern, but they are horribly slow!)
  135. if ($offset < 0) {
  136. $strlen = strlen(utf8_decode($str)); // see notes
  137. $offset = $strlen + $offset;
  138. if ($offset < 0) $offset = 0;
  139. }
  140. // establish a pattern for offset, a non-captured group equal in length to offset
  141. if ($offset > 0) {
  142. $Ox = (int)($offset/65535);
  143. $Oy = $offset%65535;
  144. if ($Ox) $offset_pattern = '(?:.{65535}){'.$Ox.'}';
  145. $offset_pattern = '^(?:'.$offset_pattern.'.{'.$Oy.'})';
  146. } else {
  147. $offset_pattern = '^'; // offset == 0; just anchor the pattern
  148. }
  149. // establish a pattern for length
  150. if (is_null($length)) {
  151. $length_pattern = '(.*)$'; // the rest of the string
  152. } else {
  153. if (!isset($strlen)) $strlen = strlen(utf8_decode($str)); // see notes
  154. if ($offset > $strlen) return ''; // another trivial case
  155. if ($length > 0) {
  156. $length = min($strlen-$offset, $length); // reduce any length that would go passed the end of the string
  157. $Lx = (int)($length/65535);
  158. $Ly = $length%65535;
  159. // +ve length requires ... a captured group of length characters
  160. if ($Lx) $length_pattern = '(?:.{65535}){'.$Lx.'}';
  161. $length_pattern = '('.$length_pattern.'.{'.$Ly.'})';
  162. } else if ($length < 0) {
  163. if ($length < ($offset - $strlen)) return '';
  164. $Lx = (int)((-$length)/65535);
  165. $Ly = (-$length)%65535;
  166. // -ve length requires ... capture everything except a group of -length characters
  167. // anchored at the tail-end of the string
  168. if ($Lx) $length_pattern = '(?:.{65535}){'.$Lx.'}';
  169. $length_pattern = '(.*)(?:'.$length_pattern.'.{'.$Ly.'})$';
  170. }
  171. }
  172. if (!preg_match('#'.$offset_pattern.$length_pattern.'#us',$str,$match)) return '';
  173. return $match[1];
  174. }
  175. }
  176. if(!function_exists('utf8_substr_replace')){
  177. /**
  178. * Unicode aware replacement for substr_replace()
  179. *
  180. * @author Andreas Gohr <andi@splitbrain.org>
  181. * @see substr_replace()
  182. */
  183. function utf8_substr_replace($string, $replacement, $start , $length=0 ){
  184. $ret = '';
  185. if($start>0) $ret .= utf8_substr($string, 0, $start);
  186. $ret .= $replacement;
  187. $ret .= utf8_substr($string, $start+$length);
  188. return $ret;
  189. }
  190. }
  191. if(!function_exists('utf8_ltrim')){
  192. /**
  193. * Unicode aware replacement for ltrim()
  194. *
  195. * @author Andreas Gohr <andi@splitbrain.org>
  196. * @see ltrim()
  197. * @return string
  198. */
  199. function utf8_ltrim($str,$charlist=''){
  200. if($charlist == '') return ltrim($str);
  201. //quote charlist for use in a characterclass
  202. $charlist = preg_replace('!([\\\\\\-\\]\\[/])!','\\\${1}',$charlist);
  203. return preg_replace('/^['.$charlist.']+/u','',$str);
  204. }
  205. }
  206. if(!function_exists('utf8_rtrim')){
  207. /**
  208. * Unicode aware replacement for rtrim()
  209. *
  210. * @author Andreas Gohr <andi@splitbrain.org>
  211. * @see rtrim()
  212. * @return string
  213. */
  214. function utf8_rtrim($str,$charlist=''){
  215. if($charlist == '') return rtrim($str);
  216. //quote charlist for use in a characterclass
  217. $charlist = preg_replace('!([\\\\\\-\\]\\[/])!','\\\${1}',$charlist);
  218. return preg_replace('/['.$charlist.']+$/u','',$str);
  219. }
  220. }
  221. if(!function_exists('utf8_trim')){
  222. /**
  223. * Unicode aware replacement for trim()
  224. *
  225. * @author Andreas Gohr <andi@splitbrain.org>
  226. * @see trim()
  227. * @return string
  228. */
  229. function utf8_trim($str,$charlist='') {
  230. if($charlist == '') return trim($str);
  231. return utf8_ltrim(utf8_rtrim($str,$charlist),$charlist);
  232. }
  233. }
  234. if(!function_exists('utf8_strtolower')){
  235. /**
  236. * This is a unicode aware replacement for strtolower()
  237. *
  238. * Uses mb_string extension if available
  239. *
  240. * @author Leo Feyer <leo@typolight.org>
  241. * @see strtolower()
  242. * @see utf8_strtoupper()
  243. */
  244. function utf8_strtolower($string){
  245. if(UTF8_MBSTRING) return mb_strtolower($string,'utf-8');
  246. global $UTF8_UPPER_TO_LOWER;
  247. return strtr($string,$UTF8_UPPER_TO_LOWER);
  248. }
  249. }
  250. if(!function_exists('utf8_strtoupper')){
  251. /**
  252. * This is a unicode aware replacement for strtoupper()
  253. *
  254. * Uses mb_string extension if available
  255. *
  256. * @author Leo Feyer <leo@typolight.org>
  257. * @see strtoupper()
  258. * @see utf8_strtoupper()
  259. */
  260. function utf8_strtoupper($string){
  261. if(UTF8_MBSTRING) return mb_strtoupper($string,'utf-8');
  262. global $UTF8_LOWER_TO_UPPER;
  263. return strtr($string,$UTF8_LOWER_TO_UPPER);
  264. }
  265. }
  266. if(!function_exists('utf8_ucfirst')){
  267. /**
  268. * UTF-8 aware alternative to ucfirst
  269. * Make a string's first character uppercase
  270. *
  271. * @author Harry Fuecks
  272. * @param string
  273. * @return string with first character as upper case (if applicable)
  274. */
  275. function utf8_ucfirst($str){
  276. switch ( utf8_strlen($str) ) {
  277. case 0:
  278. return '';
  279. case 1:
  280. return utf8_strtoupper($str);
  281. default:
  282. preg_match('/^(.{1})(.*)$/us', $str, $matches);
  283. return utf8_strtoupper($matches[1]).$matches[2];
  284. }
  285. }
  286. }
  287. if(!function_exists('utf8_ucwords')){
  288. /**
  289. * UTF-8 aware alternative to ucwords
  290. * Uppercase the first character of each word in a string
  291. *
  292. * @author Harry Fuecks
  293. * @param string
  294. * @return string with first char of each word uppercase
  295. * @see http://www.php.net/ucwords
  296. */
  297. function utf8_ucwords($str) {
  298. // Note: [\x0c\x09\x0b\x0a\x0d\x20] matches;
  299. // form feeds, horizontal tabs, vertical tabs, linefeeds and carriage returns
  300. // This corresponds to the definition of a "word" defined at http://www.php.net/ucwords
  301. $pattern = '/(^|([\x0c\x09\x0b\x0a\x0d\x20]+))([^\x0c\x09\x0b\x0a\x0d\x20]{1})[^\x0c\x09\x0b\x0a\x0d\x20]*/u';
  302. return preg_replace_callback($pattern, 'DokuWiki\UTF8\utf8_ucwords_callback',$str);
  303. }
  304. /**
  305. * Callback function for preg_replace_callback call in utf8_ucwords
  306. * You don't need to call this yourself
  307. *
  308. * @author Harry Fuecks
  309. * @param array of matches corresponding to a single word
  310. * @return string with first char of the word in uppercase
  311. * @see utf8_ucwords
  312. * @see utf8_strtoupper
  313. */
  314. function utf8_ucwords_callback($matches) {
  315. $leadingws = $matches[2];
  316. $ucfirst = utf8_strtoupper($matches[3]);
  317. $ucword = utf8_substr_replace(ltrim($matches[0]),$ucfirst,0,1);
  318. return $leadingws . $ucword;
  319. }
  320. }
  321. if(!function_exists('utf8_deaccent')){
  322. /**
  323. * Replace accented UTF-8 characters by unaccented ASCII-7 equivalents
  324. *
  325. * Use the optional parameter to just deaccent lower ($case = -1) or upper ($case = 1)
  326. * letters. Default is to deaccent both cases ($case = 0)
  327. *
  328. * @author Andreas Gohr <andi@splitbrain.org>
  329. */
  330. function utf8_deaccent($string,$case=0){
  331. if($case <= 0){
  332. global $UTF8_LOWER_ACCENTS;
  333. $string = strtr($string,$UTF8_LOWER_ACCENTS);
  334. }
  335. if($case >= 0){
  336. global $UTF8_UPPER_ACCENTS;
  337. $string = strtr($string,$UTF8_UPPER_ACCENTS);
  338. }
  339. return $string;
  340. }
  341. }
  342. if(!function_exists('utf8_romanize')){
  343. /**
  344. * Romanize a non-latin string
  345. *
  346. * @author Andreas Gohr <andi@splitbrain.org>
  347. */
  348. function utf8_romanize($string){
  349. if(utf8_isASCII($string)) return $string; //nothing to do
  350. global $UTF8_ROMANIZATION;
  351. return strtr($string,$UTF8_ROMANIZATION);
  352. }
  353. }
  354. if(!function_exists('utf8_stripspecials')){
  355. /**
  356. * Removes special characters (nonalphanumeric) from a UTF-8 string
  357. *
  358. * This function adds the controlchars 0x00 to 0x19 to the array of
  359. * stripped chars (they are not included in $UTF8_SPECIAL_CHARS)
  360. *
  361. * @author Andreas Gohr <andi@splitbrain.org>
  362. * @param string $string The UTF8 string to strip of special chars
  363. * @param string $repl Replace special with this string
  364. * @param string $additional Additional chars to strip (used in regexp char class)
  365. */
  366. function utf8_stripspecials($string,$repl='',$additional=''){
  367. global $UTF8_SPECIAL_CHARS;
  368. global $UTF8_SPECIAL_CHARS2;
  369. static $specials = null;
  370. if(is_null($specials)){
  371. #$specials = preg_quote(unicode_to_utf8($UTF8_SPECIAL_CHARS), '/');
  372. $specials = preg_quote($UTF8_SPECIAL_CHARS2, '/');
  373. }
  374. return preg_replace('/['.$additional.'\x00-\x19'.$specials.']/u',$repl,$string);
  375. }
  376. }
  377. if(!function_exists('utf8_strpos')){
  378. /**
  379. * This is an Unicode aware replacement for strpos
  380. *
  381. * @author Leo Feyer <leo@typolight.org>
  382. * @see strpos()
  383. * @param string
  384. * @param string
  385. * @param integer
  386. * @return integer
  387. */
  388. function utf8_strpos($haystack, $needle, $offset=0){
  389. $comp = 0;
  390. $length = null;
  391. while (is_null($length) || $length < $offset) {
  392. $pos = strpos($haystack, $needle, $offset + $comp);
  393. if ($pos === false)
  394. return false;
  395. $length = utf8_strlen(substr($haystack, 0, $pos));
  396. if ($length < $offset)
  397. $comp = $pos - $length;
  398. }
  399. return $length;
  400. }
  401. }
  402. if(!function_exists('utf8_tohtml')){
  403. /**
  404. * Encodes UTF-8 characters to HTML entities
  405. *
  406. * @author Tom N Harris <tnharris@whoopdedo.org>
  407. * @author <vpribish at shopping dot com>
  408. * @link http://www.php.net/manual/en/function.utf8-decode.php
  409. */
  410. function utf8_tohtml ($str) {
  411. $ret = '';
  412. foreach (utf8_to_unicode($str) as $cp) {
  413. if ($cp < 0x80)
  414. $ret .= chr($cp);
  415. elseif ($cp < 0x100)
  416. $ret .= "&#$cp;";
  417. else
  418. $ret .= '&#x'.dechex($cp).';';
  419. }
  420. return $ret;
  421. }
  422. }
  423. if(!function_exists('utf8_unhtml')){
  424. /**
  425. * Decodes HTML entities to UTF-8 characters
  426. *
  427. * Convert any &#..; entity to a codepoint,
  428. * The entities flag defaults to only decoding numeric entities.
  429. * Pass HTML_ENTITIES and named entities, including &amp; &lt; etc.
  430. * are handled as well. Avoids the problem that would occur if you
  431. * had to decode "&amp;#38;&#38;amp;#38;"
  432. *
  433. * unhtmlspecialchars(utf8_unhtml($s)) -> "&#38;&#38;"
  434. * utf8_unhtml(unhtmlspecialchars($s)) -> "&&amp#38;"
  435. * what it should be -> "&#38;&amp#38;"
  436. *
  437. * @author Tom N Harris <tnharris@whoopdedo.org>
  438. * @param string $str UTF-8 encoded string
  439. * @param boolean $entities Flag controlling decoding of named entities.
  440. * @return UTF-8 encoded string with numeric (and named) entities replaced.
  441. */
  442. function utf8_unhtml($str, $entities=null) {
  443. static $decoder = null;
  444. if (is_null($decoder))
  445. $decoder = new utf8_entity_decoder();
  446. if (is_null($entities))
  447. return preg_replace_callback('/(&#([Xx])?([0-9A-Za-z]+);)/m',
  448. 'DokuWiki\UTF8\utf8_decode_numeric', $str);
  449. else
  450. return preg_replace_callback('/&(#)?([Xx])?([0-9A-Za-z]+);/m',
  451. array(&$decoder, 'decode'), $str);
  452. }
  453. }
  454. if(!function_exists('utf8_decode_numeric')){
  455. function utf8_decode_numeric($ent) {
  456. switch ($ent[2]) {
  457. case 'X':
  458. case 'x':
  459. $cp = hexdec($ent[3]);
  460. break;
  461. default:
  462. $cp = intval($ent[3]);
  463. break;
  464. }
  465. return unicode_to_utf8(array($cp));
  466. }
  467. }
  468. if(!class_exists('utf8_entity_decoder')){
  469. class utf8_entity_decoder {
  470. var $table;
  471. function utf8_entity_decoder() {
  472. $table = get_html_translation_table(HTML_ENTITIES);
  473. $table = array_flip($table);
  474. $this->table = array_map(array(&$this,'makeutf8'), $table);
  475. }
  476. function makeutf8($c) {
  477. return unicode_to_utf8(array(ord($c)));
  478. }
  479. function decode($ent) {
  480. if ($ent[1] == '#') {
  481. return utf8_decode_numeric($ent);
  482. } elseif (array_key_exists($ent[0],$this->table)) {
  483. return $this->table[$ent[0]];
  484. } else {
  485. return $ent[0];
  486. }
  487. }
  488. }
  489. }
  490. if(!function_exists('utf8_to_unicode')){
  491. /**
  492. * Takes an UTF-8 string and returns an array of ints representing the
  493. * Unicode characters. Astral planes are supported ie. the ints in the
  494. * output can be > 0xFFFF. Occurrances of the BOM are ignored. Surrogates
  495. * are not allowed.
  496. *
  497. * If $strict is set to true the function returns false if the input
  498. * string isn't a valid UTF-8 octet sequence and raises a PHP error at
  499. * level E_USER_WARNING
  500. *
  501. * Note: this function has been modified slightly in this library to
  502. * trigger errors on encountering bad bytes
  503. *
  504. * @author <hsivonen@iki.fi>
  505. * @author Harry Fuecks <hfuecks@gmail.com>
  506. * @param string UTF-8 encoded string
  507. * @param boolean Check for invalid sequences?
  508. * @return mixed array of unicode code points or false if UTF-8 invalid
  509. * @see unicode_to_utf8
  510. * @link http://hsivonen.iki.fi/php-utf8/
  511. * @link http://sourceforge.net/projects/phputf8/
  512. */
  513. function utf8_to_unicode($str,$strict=false) {
  514. $mState = 0; // cached expected number of octets after the current octet
  515. // until the beginning of the next UTF8 character sequence
  516. $mUcs4 = 0; // cached Unicode character
  517. $mBytes = 1; // cached expected number of octets in the current sequence
  518. $out = array();
  519. $len = strlen($str);
  520. for($i = 0; $i < $len; $i++) {
  521. $in = ord($str{$i});
  522. if ( $mState == 0) {
  523. // When mState is zero we expect either a US-ASCII character or a
  524. // multi-octet sequence.
  525. if (0 == (0x80 & ($in))) {
  526. // US-ASCII, pass straight through.
  527. $out[] = $in;
  528. $mBytes = 1;
  529. } else if (0xC0 == (0xE0 & ($in))) {
  530. // First octet of 2 octet sequence
  531. $mUcs4 = ($in);
  532. $mUcs4 = ($mUcs4 & 0x1F) << 6;
  533. $mState = 1;
  534. $mBytes = 2;
  535. } else if (0xE0 == (0xF0 & ($in))) {
  536. // First octet of 3 octet sequence
  537. $mUcs4 = ($in);
  538. $mUcs4 = ($mUcs4 & 0x0F) << 12;
  539. $mState = 2;
  540. $mBytes = 3;
  541. } else if (0xF0 == (0xF8 & ($in))) {
  542. // First octet of 4 octet sequence
  543. $mUcs4 = ($in);
  544. $mUcs4 = ($mUcs4 & 0x07) << 18;
  545. $mState = 3;
  546. $mBytes = 4;
  547. } else if (0xF8 == (0xFC & ($in))) {
  548. /* First octet of 5 octet sequence.
  549. *
  550. * This is illegal because the encoded codepoint must be either
  551. * (a) not the shortest form or
  552. * (b) outside the Unicode range of 0-0x10FFFF.
  553. * Rather than trying to resynchronize, we will carry on until the end
  554. * of the sequence and let the later error handling code catch it.
  555. */
  556. $mUcs4 = ($in);
  557. $mUcs4 = ($mUcs4 & 0x03) << 24;
  558. $mState = 4;
  559. $mBytes = 5;
  560. } else if (0xFC == (0xFE & ($in))) {
  561. // First octet of 6 octet sequence, see comments for 5 octet sequence.
  562. $mUcs4 = ($in);
  563. $mUcs4 = ($mUcs4 & 1) << 30;
  564. $mState = 5;
  565. $mBytes = 6;
  566. } elseif($strict) {
  567. /* Current octet is neither in the US-ASCII range nor a legal first
  568. * octet of a multi-octet sequence.
  569. */
  570. trigger_error(
  571. 'utf8_to_unicode: Illegal sequence identifier '.
  572. 'in UTF-8 at byte '.$i,
  573. E_USER_WARNING
  574. );
  575. return false;
  576. }
  577. } else {
  578. // When mState is non-zero, we expect a continuation of the multi-octet
  579. // sequence
  580. if (0x80 == (0xC0 & ($in))) {
  581. // Legal continuation.
  582. $shift = ($mState - 1) * 6;
  583. $tmp = $in;
  584. $tmp = ($tmp & 0x0000003F) << $shift;
  585. $mUcs4 |= $tmp;
  586. /**
  587. * End of the multi-octet sequence. mUcs4 now contains the final
  588. * Unicode codepoint to be output
  589. */
  590. if (0 == --$mState) {
  591. /*
  592. * Check for illegal sequences and codepoints.
  593. */
  594. // From Unicode 3.1, non-shortest form is illegal
  595. if (((2 == $mBytes) && ($mUcs4 < 0x0080)) ||
  596. ((3 == $mBytes) && ($mUcs4 < 0x0800)) ||
  597. ((4 == $mBytes) && ($mUcs4 < 0x10000)) ||
  598. (4 < $mBytes) ||
  599. // From Unicode 3.2, surrogate characters are illegal
  600. (($mUcs4 & 0xFFFFF800) == 0xD800) ||
  601. // Codepoints outside the Unicode range are illegal
  602. ($mUcs4 > 0x10FFFF)) {
  603. if($strict){
  604. trigger_error(
  605. 'utf8_to_unicode: Illegal sequence or codepoint '.
  606. 'in UTF-8 at byte '.$i,
  607. E_USER_WARNING
  608. );
  609. return false;
  610. }
  611. }
  612. if (0xFEFF != $mUcs4) {
  613. // BOM is legal but we don't want to output it
  614. $out[] = $mUcs4;
  615. }
  616. //initialize UTF8 cache
  617. $mState = 0;
  618. $mUcs4 = 0;
  619. $mBytes = 1;
  620. }
  621. } elseif($strict) {
  622. /**
  623. *((0xC0 & (*in) != 0x80) && (mState != 0))
  624. * Incomplete multi-octet sequence.
  625. */
  626. trigger_error(
  627. 'utf8_to_unicode: Incomplete multi-octet '.
  628. ' sequence in UTF-8 at byte '.$i,
  629. E_USER_WARNING
  630. );
  631. return false;
  632. }
  633. }
  634. }
  635. return $out;
  636. }
  637. }
  638. if(!function_exists('unicode_to_utf8')){
  639. /**
  640. * Takes an array of ints representing the Unicode characters and returns
  641. * a UTF-8 string. Astral planes are supported ie. the ints in the
  642. * input can be > 0xFFFF. Occurrances of the BOM are ignored. Surrogates
  643. * are not allowed.
  644. *
  645. * If $strict is set to true the function returns false if the input
  646. * array contains ints that represent surrogates or are outside the
  647. * Unicode range and raises a PHP error at level E_USER_WARNING
  648. *
  649. * Note: this function has been modified slightly in this library to use
  650. * output buffering to concatenate the UTF-8 string (faster) as well as
  651. * reference the array by it's keys
  652. *
  653. * @param array of unicode code points representing a string
  654. * @param boolean Check for invalid sequences?
  655. * @return mixed UTF-8 string or false if array contains invalid code points
  656. * @author <hsivonen@iki.fi>
  657. * @author Harry Fuecks <hfuecks@gmail.com>
  658. * @see utf8_to_unicode
  659. * @link http://hsivonen.iki.fi/php-utf8/
  660. * @link http://sourceforge.net/projects/phputf8/
  661. */
  662. function unicode_to_utf8($arr,$strict=false) {
  663. if (!is_array($arr)) return '';
  664. ob_start();
  665. foreach (array_keys($arr) as $k) {
  666. if ( ($arr[$k] >= 0) && ($arr[$k] <= 0x007f) ) {
  667. # ASCII range (including control chars)
  668. echo chr($arr[$k]);
  669. } else if ($arr[$k] <= 0x07ff) {
  670. # 2 byte sequence
  671. echo chr(0xc0 | ($arr[$k] >> 6));
  672. echo chr(0x80 | ($arr[$k] & 0x003f));
  673. } else if($arr[$k] == 0xFEFF) {
  674. # Byte order mark (skip)
  675. // nop -- zap the BOM
  676. } else if ($arr[$k] >= 0xD800 && $arr[$k] <= 0xDFFF) {
  677. # Test for illegal surrogates
  678. // found a surrogate
  679. if($strict){
  680. trigger_error(
  681. 'unicode_to_utf8: Illegal surrogate '.
  682. 'at index: '.$k.', value: '.$arr[$k],
  683. E_USER_WARNING
  684. );
  685. return false;
  686. }
  687. } else if ($arr[$k] <= 0xffff) {
  688. # 3 byte sequence
  689. echo chr(0xe0 | ($arr[$k] >> 12));
  690. echo chr(0x80 | (($arr[$k] >> 6) & 0x003f));
  691. echo chr(0x80 | ($arr[$k] & 0x003f));
  692. } else if ($arr[$k] <= 0x10ffff) {
  693. # 4 byte sequence
  694. echo chr(0xf0 | ($arr[$k] >> 18));
  695. echo chr(0x80 | (($arr[$k] >> 12) & 0x3f));
  696. echo chr(0x80 | (($arr[$k] >> 6) & 0x3f));
  697. echo chr(0x80 | ($arr[$k] & 0x3f));
  698. } elseif($strict) {
  699. trigger_error(
  700. 'unicode_to_utf8: Codepoint out of Unicode range '.
  701. 'at index: '.$k.', value: '.$arr[$k],
  702. E_USER_WARNING
  703. );
  704. // out of range
  705. return false;
  706. }
  707. }
  708. $result = ob_get_contents();
  709. ob_end_clean();
  710. return $result;
  711. }
  712. }
  713. if(!function_exists('utf8_to_utf16be')){
  714. /**
  715. * UTF-8 to UTF-16BE conversion.
  716. *
  717. * Maybe really UCS-2 without mb_string due to utf8_to_unicode limits
  718. */
  719. function utf8_to_utf16be(&$str, $bom = false) {
  720. $out = $bom ? "\xFE\xFF" : '';
  721. if(UTF8_MBSTRING) return $out.mb_convert_encoding($str,'UTF-16BE','UTF-8');
  722. $uni = utf8_to_unicode($str);
  723. foreach($uni as $cp){
  724. $out .= pack('n',$cp);
  725. }
  726. return $out;
  727. }
  728. }
  729. if(!function_exists('utf16be_to_utf8')){
  730. /**
  731. * UTF-8 to UTF-16BE conversion.
  732. *
  733. * Maybe really UCS-2 without mb_string due to utf8_to_unicode limits
  734. */
  735. function utf16be_to_utf8(&$str) {
  736. $uni = unpack('n*',$str);
  737. return unicode_to_utf8($uni);
  738. }
  739. }
  740. if(!function_exists('utf8_bad_replace')){
  741. /**
  742. * Replace bad bytes with an alternative character
  743. *
  744. * ASCII character is recommended for replacement char
  745. *
  746. * PCRE Pattern to locate bad bytes in a UTF-8 string
  747. * Comes from W3 FAQ: Multilingual Forms
  748. * Note: modified to include full ASCII range including control chars
  749. *
  750. * @author Harry Fuecks <hfuecks@gmail.com>
  751. * @see http://www.w3.org/International/questions/qa-forms-utf-8
  752. * @param string to search
  753. * @param string to replace bad bytes with (defaults to '?') - use ASCII
  754. * @return string
  755. */
  756. function utf8_bad_replace($str, $replace = '') {
  757. $UTF8_BAD =
  758. '([\x00-\x7F]'. # ASCII (including control chars)
  759. '|[\xC2-\xDF][\x80-\xBF]'. # non-overlong 2-byte
  760. '|\xE0[\xA0-\xBF][\x80-\xBF]'. # excluding overlongs
  761. '|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}'. # straight 3-byte
  762. '|\xED[\x80-\x9F][\x80-\xBF]'. # excluding surrogates
  763. '|\xF0[\x90-\xBF][\x80-\xBF]{2}'. # planes 1-3
  764. '|[\xF1-\xF3][\x80-\xBF]{3}'. # planes 4-15
  765. '|\xF4[\x80-\x8F][\x80-\xBF]{2}'. # plane 16
  766. '|(.{1}))'; # invalid byte
  767. ob_start();
  768. while (preg_match('/'.$UTF8_BAD.'/S', $str, $matches)) {
  769. if ( !isset($matches[2])) {
  770. echo $matches[0];
  771. } else {
  772. echo $replace;
  773. }
  774. $str = substr($str,strlen($matches[0]));
  775. }
  776. $result = ob_get_contents();
  777. ob_end_clean();
  778. return $result;
  779. }
  780. }
  781. if(!function_exists('utf8_correctIdx')){
  782. /**
  783. * adjust a byte index into a utf8 string to a utf8 character boundary
  784. *
  785. * @param $str string utf8 character string
  786. * @param $i int byte index into $str
  787. * @param $next bool direction to search for boundary,
  788. * false = up (current character)
  789. * true = down (next character)
  790. *
  791. * @return int byte index into $str now pointing to a utf8 character boundary
  792. *
  793. * @author chris smith <chris@jalakai.co.uk>
  794. */
  795. function utf8_correctIdx(&$str,$i,$next=false) {
  796. if ($i <= 0) return 0;
  797. $limit = strlen($str);
  798. if ($i>=$limit) return $limit;
  799. if ($next) {
  800. while (($i<$limit) && ((ord($str[$i]) & 0xC0) == 0x80)) $i++;
  801. } else {
  802. while ($i && ((ord($str[$i]) & 0xC0) == 0x80)) $i--;
  803. }
  804. return $i;
  805. }
  806. }
  807. // only needed if no mb_string available
  808. if(!UTF8_MBSTRING){
  809. /**
  810. * UTF-8 Case lookup table
  811. *
  812. * This lookuptable defines the upper case letters to their correspponding
  813. * lower case letter in UTF-8
  814. *
  815. * @author Andreas Gohr <andi@splitbrain.org>
  816. */
  817. global $UTF8_LOWER_TO_UPPER;
  818. if(empty($UTF8_LOWER_TO_UPPER)) $UTF8_LOWER_TO_UPPER = array(
  819. "??&#x161;"=>"???","??&#x2122;"=>"??š","??&#x2DC;"=>"??¸","??&#x2014;"=>"???","??&#x2013;"=>"???","??&#x2022;"=>"???","??&#x201D;"=>"??´","??&#x201C;"=>"???","??&#x2019;"=>"???","??&#x2018;"=>"???",
  820. "???"=>"??°","???"=>"???","??&#x17D;"=>"??Ž","???"=>"??­","??&#x152;"=>"???","??&#x2039;"=>"???","??&#x160;"=>"???","??&#x2030;"=>"??Š","??&#x2C6;"=>"??¨","??&#x2021;"=>"??§",
  821. "??&#x2020;"=>"???","??&#x2026;"=>"???","??&#x201E;"=>"??¤","??&#x192;"=>"???","??&#x201A;"=>"???","?? "=>"???","á??"=>"á??","á??"=>"á??","á??"=>"á?Š","á?&#x2018;"=>"á?&#x2122;",
  822. "á??"=>"á?&#x2DC;","á?&#x192;"=>"á?&#x152;","ឞ"=>"Î&#x2122;","áž?"=>"áž?","áž?"=>"រ","ឰ"=>"ី","ឧ"=>"áž?","áž?"=>"ណ","áž?"=>"ឭ","ឤ"=>"áž?",
  823. "áž?"=>"áž?","áž?"=>"áž?","áž?"=>"ដ","áž&#x2014;"=>"áž&#x;","áž&#x2013;"=>"áž&#x17E;","áž&#x2022;"=>"áž?","áž&#x201D;"=>"áž&#x153;","áž&#x201C;"=>"áž&#x203A;","áž&#x2019;"=>"áž&#x161;","áž&#x2018;"=>"áž&#x2122;",
  824. "áž?"=>"áž&#x2DC;","áž&#x2021;"=>"áž?","áž&#x2020;"=>"áž&#x17D;","áž&#x2026;"=>"áž?","áž&#x201E;"=>"áž&#x152;","áž&#x192;"=>"áž&#x2039;","áž&#x201A;"=>"áž&#x160;","ហ"=>"áž&#x2030;","áž&#x20AC;"=>"áž&#x2C6;","á??"=>"á??",
  825. "á??"=>"á??","á??"=>"á??","á??"=>"á??","á?š"=>"á?š","á?¸"=>"á?¸","á??"=>"á?&#x203A;","á??"=>"á?&#x161;","á??"=>"á?&#x2039;","á?´"=>"á?&#x160;","á??"=>"á?&#x2030;",
  826. "á??"=>"á?&#x2C6;","á??"=>"áž?","á?°"=>"áž?","á?§"=>"á??","á??"=>"á?Ž","á??"=>"á?­","á?¤"=>"á??","á??"=>"á??","á??"=>"á??","á??"=>"á?Š",
  827. "á?&#x2014;"=>"á?&#x;","á?&#x2022;"=>"á??","á?&#x201C;"=>"á?&#x203A;","á?&#x2018;"=>"á?&#x2122;","á?&#x2026;"=>"á??","á?&#x201E;"=>"á?&#x152;","á?&#x192;"=>"á?&#x2039;","á?&#x201A;"=>"á?&#x160;","á? "=>"á?&#x2030;","á?&#x20AC;"=>"á?&#x2C6;",
  828. "á??"=>"á??","á??"=>"á?ž","á??"=>"á??","á?´"=>"á??","á??"=>"á??","á??"=>"á??","á??"=>"á?š","á?°"=>"á?¸","á?§"=>"á??","á??"=>"á?Ž",
  829. "á??"=>"á?­","á?¤"=>"á??","á??"=>"á??","á??"=>"á??","á??"=>"á?Š","á?&#x2022;"=>"á??","á?&#x201D;"=>"á?&#x153;","á?&#x201C;"=>"á?&#x203A;","á?&#x2019;"=>"á?&#x161;","á?&#x2018;"=>"á?&#x2122;",
  830. "á??"=>"á?&#x2DC;","á?&#x2021;"=>"á??","á?&#x2020;"=>"á?&#x17D;","á?&#x2026;"=>"á??","á?&#x201E;"=>"á?&#x152;","á?&#x192;"=>"á?&#x2039;","á?&#x201A;"=>"á?&#x160;","á? "=>"á?&#x2030;","á?&#x20AC;"=>"á?&#x2C6;","á?š"=>"á?¸",
  831. "á??"=>"á??","á??"=>"á?´","á??"=>"á??","á??"=>"á?°","á??"=>"á?Ž","á?­"=>"á??","á??"=>"á??","á?Š"=>"á?¨","á?§"=>"á??","á??"=>"á?¤",
  832. "á??"=>"á??","á??"=>"á? ","á?&#x;"=>"á?&#x17E;","á??"=>"á?&#x153;","á?&#x203A;"=>"á?&#x161;","á?&#x2122;"=>"á?&#x2DC;","á?&#x2014;"=>"á?&#x2013;","á?&#x2022;"=>"á?&#x201D;","á?&#x201C;"=>"á?&#x2019;","á?&#x2018;"=>"á??",
  833. "á??"=>"á?&#x17D;","á??"=>"á?&#x152;","á?&#x2039;"=>"á?&#x160;","á?&#x2030;"=>"á?&#x2C6;","á?&#x2021;"=>"á?&#x2020;","á?&#x2026;"=>"á?&#x201E;","á?&#x192;"=>"á?&#x201A;","á? "=>"á?&#x20AC;","á??"=>"á?ž","á??"=>"á??",
  834. "á??"=>"á??","á?š"=>"á?¸","á??"=>"á??","á??"=>"á?´","á??"=>"á??","á??"=>"á?°","á??"=>"á?Ž","á?­"=>"á??","á??"=>"á??","á?Š"=>"á?¨",
  835. "á?§"=>"á??","á??"=>"á?¤","á??"=>"á??","á??"=>"á? ","á?&#x203A;"=>"ᚠ","á?&#x2022;"=>"á?&#x201D;","á?&#x201C;"=>"á?&#x2019;","á?&#x2018;"=>"á??","á??"=>"á?&#x17D;","á??"=>"á?&#x152;",
  836. "á?&#x2039;"=>"á?&#x160;","á?&#x2030;"=>"á?&#x2C6;","á?&#x2021;"=>"á?&#x2020;","á?&#x2026;"=>"á?&#x201E;","á?&#x192;"=>"á?&#x201A;","á? "=>"á?&#x20AC;","áš?"=>"᚞","áš?"=>"áš?","áš?"=>"áš?","ᚚ"=>"ᚸ",
  837. "áš?"=>"áš?","áš?"=>"ᚴ","áš?"=>"áš?","áš?"=>"ᚰ","áš?"=>"ᚎ","ᚭ"=>"áš?","áš?"=>"áš?","ᚊ"=>"ᚨ","ᚧ"=>"áš?","áš?"=>"ᚤ",
  838. "áš?"=>"áš?","áš?"=>"ᚠ","áš&#x;"=>"áš&#x17E;","áš?"=>"áš&#x153;","áš&#x203A;"=>"áš&#x161;","áš&#x2122;"=>"áš&#x2DC;","áš&#x2014;"=>"áš&#x2013;","áš&#x2022;"=>"áš&#x201D;","áš&#x201C;"=>"áš&#x2019;","áš&#x2018;"=>"áš?",
  839. "áš?"=>"áš&#x17D;","áš?"=>"áš&#x152;","áš&#x2039;"=>"áš&#x160;","áš&#x2030;"=>"áš&#x2C6;","áš&#x2021;"=>"áš&#x2020;","áš&#x2026;"=>"áš&#x201E;","áš&#x192;"=>"áš&#x201A;","ᚠ"=>"áš&#x20AC;","á¸?"=>"Ḟ","á¸?"=>"á¸?",
  840. "á¸?"=>"á¸?","Ḛ"=>"Ḹ","á¸?"=>"á¸?","á¸?"=>"Ḵ","á¸?"=>"á¸?","á¸?"=>"Ḱ","á¸?"=>"Ḏ","ḭ"=>"á¸?","á¸?"=>"á¸?","Ḋ"=>"Ḩ",
  841. "ḧ"=>"á¸?","á¸?"=>"Ḥ","á¸?"=>"á¸?","á¸?"=>"Ḡ","á¸&#x;"=>"á¸&#x17E;","á¸?"=>"á¸&#x153;","á¸&#x203A;"=>"á¸&#x161;","á¸&#x2122;"=>"á¸&#x2DC;","á¸&#x2014;"=>"á¸&#x2013;","á¸&#x2022;"=>"á¸&#x201D;",
  842. "á¸&#x201C;"=>"á¸&#x2019;","á¸&#x2018;"=>"á¸?","á¸?"=>"á¸&#x17D;","á¸?"=>"á¸&#x152;","á¸&#x2039;"=>"á¸&#x160;","á¸&#x2030;"=>"á¸&#x2C6;","á¸&#x2021;"=>"á¸&#x2020;","á¸&#x2026;"=>"á¸&#x201E;","á¸&#x192;"=>"á¸&#x201A;","Ḡ"=>"á¸&#x20AC;",
  843. "Ö&#x2020;"=>"?&#x2013;","Ö&#x2026;"=>"?&#x2022;","Ö&#x201E;"=>"?&#x201D;","Ö&#x192;"=>"?&#x201C;","Ö&#x201A;"=>"?&#x2019;","Ö "=>"?&#x2018;","Ö&#x20AC;"=>"??","??"=>"??","?ž"=>"?&#x17D;","??"=>"??",
  844. "??"=>"?&#x152;","??"=>"?&#x2039;","??"=>"?&#x160;","?š"=>"?&#x2030;","?¸"=>"?&#x2C6;","??"=>"?&#x2021;","??"=>"?&#x2020;","??"=>"?&#x2026;","?´"=>"?&#x201E;","??"=>"?&#x192;",
  845. "??"=>"?&#x201A;","??"=>"? ","?°"=>"?&#x20AC;","??"=>"Ô?","?Ž"=>"Ԟ","?­"=>"Ô?","??"=>"Ô?","??"=>"Ô?","??"=>"Ô?","?Š"=>"Ԛ",
  846. "?¨"=>"Ô¸","?§"=>"Ô?","??"=>"Ô?","??"=>"Ô?","?¤"=>"Ô´","??"=>"Ô?","??"=>"Ô?","??"=>"Ô?","Ô?"=>"Ô&#x17D;","Ô?"=>"Ô&#x152;",
  847. "Ô&#x2039;"=>"Ô&#x160;","Ô&#x2030;"=>"Ô&#x2C6;","Ô&#x2021;"=>"Ô&#x2020;","Ô&#x2026;"=>"Ô&#x201E;","Ô&#x192;"=>"Ô&#x201A;","Ô "=>"Ô&#x20AC;","Ӛ"=>"Ó¸","Ó?"=>"Ó´","Ó?"=>"Ó?","Ó?"=>"Ó°",
  848. "Ó?"=>"ӎ","Ó­"=>"Ó?","Ó?"=>"Ó?","ӊ"=>"Ó¨","Ó§"=>"Ó?","Ó?"=>"Ó¤","Ó?"=>"Ó?","Ó?"=>"Ó ","Ó&#x;"=>"Ó&#x17E;","Ó?"=>"Ó&#x153;",
  849. "Ó&#x203A;"=>"Ó&#x161;","Ó&#x2122;"=>"Ó&#x2DC;","Ó&#x2014;"=>"Ó&#x2013;","Ó&#x2022;"=>"Ó&#x201D;","Ó&#x201C;"=>"Ó&#x2019;","Ó&#x2018;"=>"Ó?","Ó&#x17D;"=>"Ó?","Ó&#x152;"=>"Ó&#x2039;","Ó&#x160;"=>"Ó&#x2030;","Ó&#x2C6;"=>"Ó&#x2021;",
  850. "Ó&#x2020;"=>"Ó&#x2026;","Ó&#x201E;"=>"Ó&#x192;","Ó&#x201A;"=>"Ó ","??"=>"?ž","??"=>"??","??"=>"??","?š"=>"?¸","??"=>"??","??"=>"?´","??"=>"??",
  851. "??"=>"?°","??"=>"?Ž","?­"=>"??","??"=>"??","?Š"=>"?¨","?§"=>"??","??"=>"?¤","??"=>"??","??"=>"? ","?&#x;"=>"?&#x17E;",
  852. "??"=>"?&#x153;","?&#x203A;"=>"?&#x161;","?&#x2122;"=>"?&#x2DC;","?&#x2014;"=>"?&#x2013;","?&#x2022;"=>"?&#x201D;","?&#x201C;"=>"?&#x2019;","?&#x2018;"=>"??","??"=>"?&#x17D;","??"=>"?&#x152;","?&#x2039;"=>"?&#x160;",
  853. "? "=>"?&#x20AC;","??"=>"?ž","??"=>"??","??"=>"??","?š"=>"?¸","??"=>"??","??"=>"?´","??"=>"??","??"=>"?°","??"=>"?Ž",
  854. "?­"=>"??","??"=>"??","?Š"=>"?¨","?§"=>"??","??"=>"?¤","??"=>"??","??"=>"? ","?&#x;"=>"??","?&#x17E;"=>"?&#x17D;","??"=>"??",
  855. "?&#x153;"=>"?&#x152;","?&#x203A;"=>"?&#x2039;","?&#x161;"=>"?&#x160;","?&#x2122;"=>"?&#x2030;","?&#x2DC;"=>"?&#x2C6;","?&#x2014;"=>"?&#x2021;","?&#x2013;"=>"?&#x2020;","?&#x2022;"=>"?&#x2026;","?&#x201D;"=>"?&#x201E;","?&#x201C;"=>"?&#x192;",
  856. "?&#x2019;"=>"?&#x201A;","?&#x2018;"=>"? ","??"=>"?&#x20AC;","??"=>"??","?&#x17D;"=>"?Ž","??"=>"?­","?&#x152;"=>"??","?&#x2039;"=>"??","?&#x160;"=>"??","?&#x2030;"=>"?Š",
  857. "?&#x2C6;"=>"?¨","?&#x2021;"=>"?§","?&#x2020;"=>"??","?&#x2026;"=>"??","?&#x201E;"=>"?¤","?&#x192;"=>"??","?&#x201A;"=>"??","? "=>"??","?&#x20AC;"=>"? ","??"=>"?&#x;",
  858. "?ž"=>"?&#x17E;","??"=>"??","??"=>"?&#x153;","??"=>"?&#x203A;","??"=>"?&#x161;","?š"=>"?&#x2122;","?¸"=>"?&#x2DC;","??"=>"?&#x2014;","??"=>"?&#x2013;","??"=>"?&#x2022;",
  859. "?´"=>"?&#x201D;","??"=>"?&#x201C;","??"=>"?&#x2019;","??"=>"?&#x2018;","?°"=>"??","??"=>"Î&#x2022;","??"=>"Î?","??"=>"Î?","?°"=>"Î&#x161;","??"=>"?Ž",
  860. "?­"=>"??","??"=>"??","?Š"=>"?¨","?§"=>"??","??"=>"?¤","??"=>"??","??"=>"? ","?&#x;"=>"?&#x17E;","??"=>"?&#x153;","?&#x203A;"=>"?&#x161;",
  861. "?&#x2122;"=>"?&#x2DC;","?&#x2013;"=>"Π","?&#x2022;"=>"Î?","?&#x2018;"=>"Î&#x2DC;","??"=>"Î&#x2019;","?&#x17D;"=>"Î?","??"=>"Î&#x17D;","?&#x152;"=>"Î&#x152;","?&#x2039;"=>"Î?","?&#x160;"=>"Î?",
  862. "?&#x2030;"=>"Ί","?&#x2C6;"=>"Ψ","?&#x2021;"=>"Χ","?&#x2020;"=>"Î?","?&#x2026;"=>"Î?","?&#x201E;"=>"Τ","?&#x192;"=>"Î?","?&#x201A;"=>"Î?","? "=>"Î?","?&#x20AC;"=>"Π",
  863. "Î?"=>"Î&#x;","Ξ"=>"Î&#x17E;","Î?"=>"Î?","Î?"=>"Î&#x153;","Î?"=>"Î&#x203A;","Î?"=>"Î&#x161;","Κ"=>"Î&#x2122;","θ"=>"Î&#x2DC;","Î?"=>"Î&#x2014;","Î?"=>"Î&#x2013;",
  864. "Î?"=>"Î&#x2022;","δ"=>"Î&#x201D;","Î?"=>"Î&#x201C;","Î?"=>"Î&#x2019;","Î?"=>"Î&#x2018;","Î?"=>"Î&#x160;","Ύ"=>"Î&#x2030;","έ"=>"Î&#x2C6;","Î?"=>"Î&#x2020;","?&#x2019;"=>"??",
  865. "?&#x2039;"=>"??","?&#x160;"=>"??","?&#x2C6;"=>"?Ž","?&#x192;"=>"?Š","?&#x20AC;"=>"??","É?"=>"?&#x;","É?"=>"??","É?"=>"?&#x153;","Ɋ"=>"?&#x2013;","ɨ"=>"?&#x2014;",
  866. "É?"=>"?&#x201D;","É&#x203A;"=>"??","É&#x2122;"=>"??","É&#x2014;"=>"?&#x160;","É&#x2013;"=>"?&#x2030;","É&#x201D;"=>"?&#x2020;","É&#x201C;"=>"? ","??"=>"??","??"=>"?°","??"=>"?Ž",
  867. "?­"=>"??","??"=>"??","?Š"=>"?¨","?§"=>"??","??"=>"?¤","??"=>"??","?&#x;"=>"?&#x17E;","??"=>"?&#x153;","?&#x203A;"=>"?&#x161;","?&#x2122;"=>"?&#x2DC;",
  868. "?&#x2014;"=>"?&#x2013;","?&#x2022;"=>"?&#x201D;","?&#x201C;"=>"?&#x2019;","?&#x2018;"=>"??","??"=>"?&#x17D;","??"=>"?&#x152;","?&#x2039;"=>"?&#x160;","?&#x2030;"=>"?&#x2C6;","?&#x2021;"=>"?&#x2020;","?&#x2026;"=>"?&#x201E;",
  869. "?&#x192;"=>"?&#x201A;","? "=>"?&#x20AC;","Ç?"=>"Ǟ","Ç?"=>"Ç?","Ç?"=>"Ç?","ǚ"=>"Ǹ","Ç?"=>"Ç´","Ç?"=>"Ç?","Ç?"=>"ǎ","Ç­"=>"Ç?",
  870. "Ç?"=>"Ç?","NJ"=>"Ǩ","ǧ"=>"Ç?","Ç?"=>"Ǥ","Ç?"=>"Ç?","Ç?"=>"Ç ","Ç&#x;"=>"Ç&#x17E;","Ç?"=>"?&#x17D;","Ç&#x153;"=>"Ç&#x203A;","Ç&#x161;"=>"Ç&#x2122;",
  871. "Ç&#x2DC;"=>"Ç&#x2014;","Ç&#x2013;"=>"Ç&#x2022;","Ç&#x201D;"=>"Ç&#x201C;","Ç&#x2019;"=>"Ç&#x2018;","Ç?"=>"Ç?","Ç&#x17D;"=>"Ç?","Ç&#x152;"=>"Ç&#x2039;","Ç&#x2030;"=>"Ç&#x2C6;","Ç&#x2020;"=>"Ç&#x2026;","??"=>"Ç?",
  872. "??"=>"??","?š"=>"?¸","??"=>"??","?´"=>"??","?°"=>"??","?­"=>"??","?¨"=>"?§","??"=>"?¤","??"=>"??","??"=>"? ",
  873. "?&#x17E;"=>"? ","?&#x2122;"=>"?&#x2DC;","?&#x2022;"=>"Ç?","?&#x2019;"=>"?&#x2018;","?&#x152;"=>"?&#x2039;","?&#x2C6;"=>"?&#x2021;","?&#x2026;"=>"?&#x201E;","?&#x192;"=>"?&#x201A;","??"=>"S","?ž"=>"??",
  874. "??"=>"??","??"=>"?š","??"=>"??","??"=>"?´","??"=>"??","??"=>"?°","??"=>"?Ž","?­"=>"??","??"=>"??","?Š"=>"?¨",
  875. "?§"=>"??","??"=>"?¤","??"=>"??","??"=>"? ","?&#x;"=>"?&#x17E;","??"=>"?&#x153;","?&#x203A;"=>"?&#x161;","?&#x2122;"=>"?&#x2DC;","?&#x2014;"=>"?&#x2013;","?&#x2022;"=>"?&#x201D;",
  876. "?&#x201C;"=>"?&#x2019;","?&#x2018;"=>"??","??"=>"?&#x17D;","??"=>"?&#x152;","?&#x2039;"=>"?&#x160;","?&#x2C6;"=>"?&#x2021;","?&#x2020;"=>"?&#x2026;","?&#x201E;"=>"?&#x192;","?&#x201A;"=>"? ","?&#x20AC;"=>"Ä?",
  877. "Ğ"=>"Ä?","Ä?"=>"Ä?","Ä?"=>"Ě","Ä?"=>"Ä?","Ä?"=>"Ä´","Ä?"=>"Ä?","Ä?"=>"I","Ä?"=>"Ď","Ä­"=>"Ä?","Ä?"=>"Ä?",
  878. "Ċ"=>"Ĩ","ħ"=>"Ä?","Ä?"=>"Ĥ","Ä?"=>"Ä?","Ä?"=>"Ä ","Ä&#x;"=>"Ä&#x17E;","Ä?"=>"Ä&#x153;","Ä&#x203A;"=>"Ä&#x161;","Ä&#x2122;"=>"Ä&#x2DC;","Ä&#x2014;"=>"Ä&#x2013;",
  879. "Ä&#x2022;"=>"Ä&#x201D;","Ä&#x201C;"=>"Ä&#x2019;","Ä&#x2018;"=>"Ä?","Ä?"=>"Ä&#x17D;","Ä?"=>"Ä&#x152;","Ä&#x2039;"=>"Ä&#x160;","Ä&#x2030;"=>"Ä&#x2C6;","Ä&#x2021;"=>"Ä&#x2020;","Ä&#x2026;"=>"Ä&#x201E;","Ä&#x192;"=>"Ä&#x201A;",
  880. "Ä "=>"Ä&#x20AC;","??"=>"?¸","?ž"=>"?&#x17E;","??"=>"??","??"=>"?&#x153;","??"=>"?&#x203A;","??"=>"?&#x161;","?š"=>"?&#x2122;","?¸"=>"?&#x2DC;","??"=>"?&#x2013;",
  881. "??"=>"?&#x2022;","?´"=>"?&#x201D;","??"=>"?&#x201C;","??"=>"?&#x2019;","??"=>"?&#x2018;","?°"=>"??","??"=>"??","?Ž"=>"?&#x17D;","?­"=>"??","??"=>"?&#x152;",
  882. "??"=>"?&#x2039;","??"=>"?&#x160;","?Š"=>"?&#x2030;","?¨"=>"?&#x2C6;","?§"=>"?&#x2021;","??"=>"?&#x2020;","??"=>"?&#x2026;","?¤"=>"?&#x201E;","??"=>"?&#x192;","??"=>"?&#x201A;",
  883. "??"=>"? ","? "=>"?&#x20AC;","Â?"=>"Î&#x153;","z"=>"Z","y"=>"Y","x"=>"X","w"=>"W","v"=>"V","u"=>"U","t"=>"T",
  884. "s"=>"S","r"=>"R","q"=>"Q","p"=>"P","o"=>"O","n"=>"N","m"=>"M","l"=>"L","k"=>"K","j"=>"J",
  885. "i"=>"I","h"=>"H","g"=>"G","f"=>"F","e"=>"E","d"=>"D","c"=>"C","b"=>"B","a"=>"A"
  886. );
  887. /**
  888. * UTF-8 Case lookup table
  889. *
  890. * This lookuptable defines the lower case letters to their correspponding
  891. * upper case letter in UTF-8
  892. *
  893. * @author Andreas Gohr <andi@splitbrain.org>
  894. */
  895. global $UTF8_UPPER_TO_LOWER;
  896. if(empty($UTF8_UPPER_TO_LOWER)) $UTF8_UPPER_TO_LOWER = array (
  897. "???"=>"??&#x161;","??š"=>"??&#x2122;","??¸"=>"??&#x2DC;","???"=>"??&#x2014;","???"=>"??&#x2013;","???"=>"??&#x2022;","??´"=>"??&#x201D;","???"=>"??&#x201C;","???"=>"??&#x2019;","???"=>"??&#x2018;",
  898. "??°"=>"???","???"=>"???","??Ž"=>"??&#x17D;","??­"=>"???","???"=>"??&#x152;","???"=>"??&#x2039;","???"=>"??&#x160;","??Š"=>"??&#x2030;","??¨"=>"??&#x2C6;","??§"=>"??&#x2021;",
  899. "???"=>"??&#x2020;","???"=>"??&#x2026;","??¤"=>"??&#x201E;","???"=>"??&#x192;","???"=>"??&#x201A;","???"=>"?? ","á??"=>"á??","á??"=>"á??","á?Š"=>"á??","á?&#x2122;"=>"á?&#x2018;",
  900. "á?&#x2DC;"=>"á??","á?&#x152;"=>"á?&#x192;","Î&#x2122;"=>"ឞ","áž?"=>"áž?","រ"=>"áž?","ី"=>"ឰ","áž?"=>"ឧ","ណ"=>"áž?","ឭ"=>"áž?","áž?"=>"ឤ",
  901. "áž?"=>"áž?","áž?"=>"áž?","ដ"=>"áž?","áž&#x;"=>"áž&#x2014;","áž&#x17E;"=>"áž&#x2013;","áž?"=>"áž&#x2022;","áž&#x153;"=>"áž&#x201D;","áž&#x203A;"=>"áž&#x201C;","áž&#x161;"=>"áž&#x2019;","áž&#x2122;"=>"áž&#x2018;",
  902. "áž&#x2DC;"=>"áž?","áž?"=>"áž&#x2021;","áž&#x17D;"=>"áž&#x2020;","áž?"=>"áž&#x2026;","áž&#x152;"=>"áž&#x201E;","áž&#x2039;"=>"áž&#x192;","áž&#x160;"=>"áž&#x201A;","áž&#x2030;"=>"ហ","áž&#x2C6;"=>"áž&#x20AC;","á??"=>"á??",
  903. "á??"=>"á??","á??"=>"á??","á??"=>"á??","á?š"=>"á?š","á?¸"=>"á?¸","á?&#x203A;"=>"á??","á?&#x161;"=>"á??","á?&#x2039;"=>"á??","á?&#x160;"=>"á?´","á?&#x2030;"=>"á??",
  904. "á?&#x2C6;"=>"á??","áž?"=>"á??","áž?"=>"á?°","á??"=>"á?§","á?Ž"=>"á??","á?­"=>"á??","á??"=>"á?¤","á??"=>"á??","á??"=>"á??","á?Š"=>"á??",
  905. "á?&#x;"=>"á?&#x2014;","á??"=>"á?&#x2022;","á?&#x203A;"=>"á?&#x201C;","á?&#x2122;"=>"á?&#x2018;","á??"=>"á?&#x2026;","á?&#x152;"=>"á?&#x201E;","á?&#x2039;"=>"á?&#x192;","á?&#x160;"=>"á?&#x201A;","á?&#x2030;"=>"á? ","á?&#x2C6;"=>"á?&#x20AC;",
  906. "á??"=>"á??","á?ž"=>"á??","á??"=>"á??","á??"=>"á?´","á??"=>"á??","á??"=>"á??","á?š"=>"á??","á?¸"=>"á?°","á??"=>"á?§","á?Ž"=>"á??",
  907. "á?­"=>"á??","á??"=>"á?¤","á??"=>"á??","á??"=>"á??","á?Š"=>"á??","á??"=>"á?&#x2022;","á?&#x153;"=>"á?&#x201D;","á?&#x203A;"=>"á?&#x201C;","á?&#x161;"=>"á?&#x2019;","á?&#x2122;"=>"á?&#x2018;",
  908. "á?&#x2DC;"=>"á??","á??"=>"á?&#x2021;","á?&#x17D;"=>"á?&#x2020;","á??"=>"á?&#x2026;","á?&#x152;"=>"á?&#x201E;","á?&#x2039;"=>"á?&#x192;","á?&#x160;"=>"á?&#x201A;","á?&#x2030;"=>"á? ","á?&#x2C6;"=>"á?&#x20AC;","á?¸"=>"á?š",
  909. "á??"=>"á??","á?´"=>"á??","á??"=>"á??","á?°"=>"á??","á?Ž"=>"á??","á??"=>"á?­","á??"=>"á??","á?¨"=>"á?Š","á??"=>"á?§","á?¤"=>"á??",
  910. "á??"=>"á??","á? "=>"á??","á?&#x17E;"=>"á?&#x;","á?&#x153;"=>"á??","á?&#x161;"=>"á?&#x203A;","á?&#x2DC;"=>"á?&#x2122;","á?&#x2013;"=>"á?&#x2014;","á?&#x201D;"=>"á?&#x2022;","á?&#x2019;"=>"á?&#x201C;","á??"=>"á?&#x2018;",
  911. "á?&#x17D;"=>"á??","á?&#x152;"=>"á??","á?&#x160;"=>"á?&#x2039;","á?&#x2C6;"=>"á?&#x2030;","á?&#x2020;"=>"á?&#x2021;","á?&#x201E;"=>"á?&#x2026;","á?&#x201A;"=>"á?&#x192;","á?&#x20AC;"=>"á? ","á?ž"=>"á??","á??"=>"á??",
  912. "á??"=>"á??","á?¸"=>"á?š","á??"=>"á??","á?´"=>"á??","á??"=>"á??","á?°"=>"á??","á?Ž"=>"á??","á??"=>"á?­","á??"=>"á??","á?¨"=>"á?Š",
  913. "á??"=>"á?§","á?¤"=>"á??","á??"=>"á??","á? "=>"á??","ᚠ"=>"á?&#x203A;","á?&#x201D;"=>"á?&#x2022;","á?&#x2019;"=>"á?&#x201C;","á??"=>"á?&#x2018;","á?&#x17D;"=>"á??","á?&#x152;"=>"á??",
  914. "á?&#x160;"=>"á?&#x2039;","á?&#x2C6;"=>"á?&#x2030;","á?&#x2020;"=>"á?&#x2021;","á?&#x201E;"=>"á?&#x2026;","á?&#x201A;"=>"á?&#x192;","á?&#x20AC;"=>"á? ","᚞"=>"áš?","áš?"=>"áš?","áš?"=>"áš?","ᚸ"=>"ᚚ",
  915. "áš?"=>"áš?","ᚴ"=>"áš?","áš?"=>"áš?","ᚰ"=>"áš?","ᚎ"=>"áš?","áš?"=>"ᚭ","áš?"=>"áš?","ᚨ"=>"ᚊ","áš?"=>"ᚧ","ᚤ"=>"áš?",
  916. "áš?"=>"áš?","ᚠ"=>"áš?","áš&#x17E;"=>"áš&#x;","áš&#x153;"=>"áš?","áš&#x161;"=>"áš&#x203A;","áš&#x2DC;"=>"áš&#x2122;","áš&#x2013;"=>"áš&#x2014;","áš&#x201D;"=>"áš&#x2022;","áš&#x2019;"=>"áš&#x201C;","áš?"=>"áš&#x2018;",
  917. "áš&#x17D;"=>"áš?","áš&#x152;"=>"áš?","áš&#x160;"=>"áš&#x2039;","áš&#x2C6;"=>"áš&#x2030;","áš&#x2020;"=>"áš&#x2021;","áš&#x201E;"=>"áš&#x2026;","áš&#x201A;"=>"áš&#x192;","áš&#x20AC;"=>"ᚠ","Ḟ"=>"á¸?","á¸?"=>"á¸?",
  918. "á¸?"=>"á¸?","Ḹ"=>"Ḛ","á¸?"=>"á¸?","Ḵ"=>"á¸?","á¸?"=>"á¸?…

Large files files are truncated, but you can click here to view the full file