PageRenderTime 70ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/dokuwiki/html/inc/utf8.php

http://portablenginxdokuwik.codeplex.com
PHP | 1683 lines | 1013 code | 152 blank | 518 comment | 168 complexity | 965a4bb72bd4f2927804953e18c4cfe8 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0

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

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

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