multibyte_string_functions_internal.lib.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. <?php
  2. /**
  3. * ==============================================================================
  4. * File: multibyte_string_functions_internal.lib.php
  5. * Main API extension library for Dokeos 1.8.6+ LMS,
  6. * contains functions for internal use only.
  7. * License: GNU/GPL version 2 or later (Free Software Foundation)
  8. * @author: Ivan Tcholakov, ivantcholakov@gmail.com, 2009
  9. * @package dokeos.library
  10. * ==============================================================================
  11. */
  12. /**
  13. * ----------------------------------------------------------------------------
  14. * Appendix to "Multibyte string conversion functions"
  15. * ----------------------------------------------------------------------------
  16. */
  17. // This is a php-implementation of the function api_convert_encoding().
  18. function _api_convert_encoding($string, $to_encoding, $from_encoding) {
  19. static $character_map = array();
  20. static $utf8_like = array('UTF-8', 'US-ASCII');
  21. if (empty($string)) {
  22. return $string;
  23. }
  24. $to_encoding = api_refine_encoding_id($to_encoding);
  25. $from_encoding = api_refine_encoding_id($from_encoding);
  26. if (api_equal_encodings($to_encoding, $from_encoding)) {
  27. return $string;
  28. }
  29. $to = _api_get_character_map_name($to_encoding);
  30. $from = _api_get_character_map_name($from_encoding);
  31. if (empty($to) || empty($from) || $to == $from || (in_array($to, $utf8_like) && in_array($from, $utf8_like))) {
  32. return $string;
  33. }
  34. if (!isset($character_map[$to])) {
  35. $character_map[$to] = _api_parse_character_map($to);
  36. }
  37. if ($character_map[$to] === false) {
  38. return $string;
  39. }
  40. if (!isset($character_map[$from])) {
  41. $character_map[$from] = _api_parse_character_map($from);
  42. }
  43. if ($character_map[$from] === false) {
  44. return $string;
  45. }
  46. if ($from != 'UTF-8') {
  47. $len = api_byte_count($string);
  48. $codepoints = array();
  49. for ($i = 0; $i < $len; $i++) {
  50. $ord = ord($string[$i]);
  51. if ($ord > 127) {
  52. if (isset($character_map[$from]['local'][$ord])) {
  53. $codepoints[] = $character_map[$from]['local'][$ord];
  54. } else {
  55. $codepoints[] = 0xFFFD; // U+FFFD REPLACEMENT CHARACTER is the general substitute character in the Unicode Standard.
  56. }
  57. } else {
  58. $codepoints[] = $ord;
  59. }
  60. }
  61. } else {
  62. $codepoints = _api_utf8_to_unicode($string);
  63. }
  64. if ($to != 'UTF-8') {
  65. foreach ($codepoints as $i => &$codepoint) {
  66. if ($codepoint > 127) {
  67. if (isset($character_map[$from]['local'][$codepoint])) {
  68. $codepoint = chr($character_map[$from]['local'][$codepoint]);
  69. } else {
  70. $codepoint = '?'; // Unknown character.
  71. }
  72. } else {
  73. $codepoint = chr($codepoint);
  74. }
  75. }
  76. $string = implode($codepoints);
  77. } else {
  78. $string = _api_utf8_from_unicode($codepoints);
  79. }
  80. return $string;
  81. }
  82. // This function determines the name of the conversion table, dealing with
  83. // aliases if the encoding identificator.
  84. function _api_get_character_map_name($encoding) {
  85. static $character_map_selector;
  86. if (!isset($character_map_selector)) {
  87. $file = dirname(__FILE__) . '/multibyte_string_database/conversion/character_map_selector.php';
  88. if (file_exists($file)) {
  89. $character_map_selector = include ($file);
  90. } else {
  91. $character_map_selector = array();
  92. }
  93. }
  94. return isset($character_map_selector[$encoding]) ? $character_map_selector[$encoding] : '';
  95. }
  96. // This function parses a given conversion table (a text file) and creates in the memory
  97. // two tables for conversion - character set from/to Unicode codepoints.
  98. function &_api_parse_character_map($name) {
  99. $result = array('local' => array(), 'unicode' => array());
  100. $file = dirname(__FILE__) . '/multibyte_string_database/conversion/' . $name . '.TXT';
  101. if (file_exists($file)) {
  102. $text = @file_get_contents($file);
  103. if ($text !== false) {
  104. $text = explode(chr(10), $text);
  105. foreach ($text as $line) {
  106. if (empty($line)) {
  107. continue;
  108. }
  109. if (!empty($line) && trim($line) && $line[0] != '#') {
  110. $matches = array();
  111. preg_match('/[[:space:]]*0x([[:alnum:]]*)[[:space:]]+0x([[:alnum:]]*)[[:space:]]+/', $line, $matches);
  112. $ord = hexdec(trim($matches[1]));
  113. if ($ord > 127) {
  114. $result['local'][$ord] = hexdec(trim($matches[2]));
  115. $result['unicode'][$result['local'][$ord]] = $ord;
  116. }
  117. }
  118. }
  119. } else {
  120. return false ;
  121. }
  122. } else {
  123. return false;
  124. }
  125. return $result;
  126. }
  127. /**
  128. * Takes an UTF-8 string and returns an array of ints representing the
  129. * Unicode characters. Astral planes are supported ie. the ints in the
  130. * output can be > 0xFFFF. Occurrances of the BOM are ignored. Surrogates
  131. * are not allowed.
  132. * @param string $string The UTF-8 encoded string.
  133. * @return array Returns an array of unicode code points.
  134. * @author Henri Sivonen, mailto:hsivonen@iki.fi
  135. * @link http://hsivonen.iki.fi/php-utf8/
  136. * @author Ivan Tcholakov, 2009, modifications for the Dokeos LMS.
  137. */
  138. function _api_utf8_to_unicode($string) {
  139. $state = 0; // cached expected number of octets after the current octet
  140. // until the beginning of the next UTF8 character sequence
  141. $codepoint = 0; // cached Unicode character
  142. $bytes = 1; // cached expected number of octets in the current sequence
  143. $result = array();
  144. $len = api_byte_count($string);
  145. for ($i = 0; $i < $len; $i++) {
  146. $byte = ord($string[$i]);
  147. if ($state == 0) {
  148. // When state is zero we expect either a US-ASCII character or a
  149. // multi-octet sequence.
  150. if (0 == (0x80 & ($byte))) {
  151. // US-ASCII, pass straight through.
  152. $result[] = $byte;
  153. $bytes = 1;
  154. } else if (0xC0 == (0xE0 & ($byte))) {
  155. // First octet of 2 octet sequence
  156. $codepoint = ($byte);
  157. $codepoint = ($codepoint & 0x1F) << 6;
  158. $state = 1;
  159. $bytes = 2;
  160. } else if (0xE0 == (0xF0 & ($byte))) {
  161. // First octet of 3 octet sequence
  162. $codepoint = ($byte);
  163. $codepoint = ($codepoint & 0x0F) << 12;
  164. $state = 2;
  165. $bytes = 3;
  166. } else if (0xF0 == (0xF8 & ($byte))) {
  167. // First octet of 4 octet sequence
  168. $codepoint = ($byte);
  169. $codepoint = ($codepoint & 0x07) << 18;
  170. $state = 3;
  171. $bytes = 4;
  172. } else if (0xF8 == (0xFC & ($byte))) {
  173. // First octet of 5 octet sequence.
  174. // This is illegal because the encoded codepoint must be either
  175. // (a) not the shortest form or
  176. // (b) outside the Unicode range of 0-0x10FFFF.
  177. // Rather than trying to resynchronize, we will carry on until the end
  178. // of the sequence and let the later error handling code catch it.
  179. $codepoint = ($byte);
  180. $codepoint = ($codepoint & 0x03) << 24;
  181. $state = 4;
  182. $bytes = 5;
  183. } else if (0xFC == (0xFE & ($byte))) {
  184. // First octet of 6 octet sequence, see comments for 5 octet sequence.
  185. $codepoint = ($byte);
  186. $codepoint = ($codepoint & 1) << 30;
  187. $state = 5;
  188. $bytes = 6;
  189. } else {
  190. // Current octet is neither in the US-ASCII range nor a legal first
  191. // octet of a multi-octet sequence.
  192. $state = 0;
  193. $codepoint = 0;
  194. $bytes = 1;
  195. $result[] = 0xFFFD; // U+FFFD REPLACEMENT CHARACTER is the general substitute character in the Unicode Standard.
  196. continue ;
  197. }
  198. } else {
  199. // When state is non-zero, we expect a continuation of the multi-octet
  200. // sequence
  201. if (0x80 == (0xC0 & ($byte))) {
  202. // Legal continuation.
  203. $shift = ($state - 1) * 6;
  204. $tmp = $byte;
  205. $tmp = ($tmp & 0x0000003F) << $shift;
  206. $codepoint |= $tmp;
  207. // End of the multi-octet sequence. $codepoint now contains the final
  208. // Unicode codepoint to be output
  209. if (0 == --$state) {
  210. // Check for illegal sequences and codepoints.
  211. // From Unicode 3.1, non-shortest form is illegal
  212. if (((2 == $bytes) && ($codepoint < 0x0080)) ||
  213. ((3 == $bytes) && ($codepoint < 0x0800)) ||
  214. ((4 == $bytes) && ($codepoint < 0x10000)) ||
  215. (4 < $bytes) ||
  216. // From Unicode 3.2, surrogate characters are illegal
  217. (($codepoint & 0xFFFFF800) == 0xD800) ||
  218. // Codepoints outside the Unicode range are illegal
  219. ($codepoint > 0x10FFFF)) {
  220. $state = 0;
  221. $codepoint = 0;
  222. $bytes = 1;
  223. $result[] = 0xFFFD;
  224. continue ;
  225. }
  226. if (0xFEFF != $codepoint) {
  227. // BOM is legal but we don't want to output it
  228. $result[] = $codepoint;
  229. }
  230. // Initialize UTF8 cache
  231. $state = 0;
  232. $codepoint = 0;
  233. $bytes = 1;
  234. }
  235. } else {
  236. // ((0xC0 & (*in) != 0x80) && (state != 0))
  237. // Incomplete multi-octet sequence.
  238. $state = 0;
  239. $codepoint = 0;
  240. $bytes = 1;
  241. $result[] = 0xFFFD;
  242. }
  243. }
  244. }
  245. return $result;
  246. }
  247. /**
  248. * Takes an array of ints representing the Unicode characters and returns a UTF-8 string.
  249. * @param array $codepoints An array of unicode code points representing a string.
  250. * @return string Returns a UTF-8 string constructed using the given code points.
  251. */
  252. function _api_utf8_from_unicode($codepoints) {
  253. return implode(array_map('_api_utf8_chr', $codepoints));
  254. }
  255. /**
  256. * Takes an integer value (codepoint) and returns its correspondent representing the Unicode character.
  257. * Astral planes are supported, ie the intger input can be > 0xFFFF. Occurrances of the BOM are ignored.
  258. * Surrogates are not allowed.
  259. * @param array $array An array of unicode code points representing a string
  260. * @return string Returns the corresponding UTF-8 character.
  261. * @author Henri Sivonen, mailto:hsivonen@iki.fi
  262. * @link http://hsivonen.iki.fi/php-utf8/
  263. * @author Ivan Tcholakov, 2009, modifications for the Dokeos LMS.
  264. * @see _api_utf8_from_unicode()
  265. * This is a UTF-8 aware version of the function chr().
  266. * @link http://php.net/manual/en/function.chr.php
  267. */
  268. function _api_utf8_chr($codepoint) {
  269. // ASCII range (including control chars)
  270. if ( ($codepoint >= 0) && ($codepoint <= 0x007f) ) {
  271. $result = chr($codepoint);
  272. // 2 byte sequence
  273. } else if ($codepoint <= 0x07ff) {
  274. $result = chr(0xc0 | ($codepoint >> 6)) . chr(0x80 | ($codepoint & 0x003f));
  275. // Byte order mark (skip)
  276. } else if($codepoint == 0xFEFF) {
  277. // nop -- zap the BOM
  278. $result = '';
  279. // Test for illegal surrogates
  280. } else if ($codepoint >= 0xD800 && $codepoint <= 0xDFFF) {
  281. // found a surrogate
  282. $result = _api_utf8_chr(0xFFFD); // U+FFFD REPLACEMENT CHARACTER is the general substitute character in the Unicode Standard.
  283. // 3 byte sequence
  284. } else if ($codepoint <= 0xffff) {
  285. $result = chr(0xe0 | ($codepoint >> 12)) . chr(0x80 | (($codepoint >> 6) & 0x003f)) . chr(0x80 | ($codepoint & 0x003f));
  286. // 4 byte sequence
  287. } else if ($codepoint <= 0x10ffff) {
  288. $result = chr(0xf0 | ($codepoint >> 18)) . chr(0x80 | (($codepoint >> 12) & 0x3f)) . chr(0x80 | (($codepoint >> 6) & 0x3f)) . chr(0x80 | ($codepoint & 0x3f));
  289. } else {
  290. // out of range
  291. $result = _api_utf8_chr(0xFFFD);
  292. }
  293. return $result;
  294. }
  295. /**
  296. * Takes the first UTF-8 character in a string and returns its codepoint (integer).
  297. * @param string $utf8_character The UTF-8 encoded character.
  298. * @return int Returns: the codepoint; or 0xFFFD (unknown character) when the input string is empty.
  299. * This is a UTF-8 aware version of the function ord().
  300. * @link http://php.net/manual/en/function.ord.php
  301. * Note about a difference with the original funtion ord(): ord('') returns 0.
  302. */
  303. function _api_utf8_ord($utf8_character) {
  304. if (empty($utf8_character)) {
  305. return 0xFFFD;
  306. }
  307. $codepoints = _api_utf8_to_unicode($utf8_character);
  308. return $codepoints[0];
  309. }
  310. /**
  311. * ----------------------------------------------------------------------------
  312. * Appendix to "Common multibyte string functions"
  313. * ----------------------------------------------------------------------------
  314. */
  315. // Reads case folding properties about a given character from a file-based "database".
  316. function _api_utf8_get_letter_case_properties($codepoint, $type = 'lower') {
  317. static $config = array();
  318. static $range = array();
  319. if (!isset($range[$codepoint])) {
  320. if ($codepoint > 128 && $codepoint < 256) {
  321. $range[$codepoint] = '0080_00ff'; // Latin-1 Supplement
  322. } elseif ($codepoint < 384) {
  323. $range[$codepoint] = '0100_017f'; // Latin Extended-A
  324. } elseif ($codepoint < 592) {
  325. $range[$codepoint] = '0180_024F'; // Latin Extended-B
  326. } elseif ($codepoint < 688) {
  327. $range[$codepoint] = '0250_02af'; // IPA Extensions
  328. } elseif ($codepoint >= 880 && $codepoint < 1024) {
  329. $range[$codepoint] = '0370_03ff'; // Greek and Coptic
  330. } elseif ($codepoint < 1280) {
  331. $range[$codepoint] = '0400_04ff'; // Cyrillic
  332. } elseif ($codepoint < 1328) {
  333. $range[$codepoint] = '0500_052f'; // Cyrillic Supplement
  334. } elseif ($codepoint < 1424) {
  335. $range[$codepoint] = '0530_058f'; // Armenian
  336. } elseif ($codepoint >= 7680 && $codepoint < 7936) {
  337. $range[$codepoint] = '1e00_1eff'; // Latin Extended Additional
  338. } elseif ($codepoint < 8192) {
  339. $range[$codepoint] = '1f00_1fff'; // Greek Extended
  340. } elseif ($codepoint >= 8448 && $codepoint < 8528) {
  341. $range[$codepoint] = '2100_214f'; // Letterlike Symbols
  342. } elseif ($codepoint < 8592) {
  343. $range[$codepoint] = '2150_218f'; // Number Forms
  344. } elseif ($codepoint >= 9312 && $codepoint < 9472) {
  345. $range[$codepoint] = '2460_24ff'; // Enclosed Alphanumerics
  346. } elseif ($codepoint >= 11264 && $codepoint < 11360) {
  347. $range[$codepoint] = '2c00_2c5f'; // Glagolitic
  348. } elseif ($codepoint < 11392) {
  349. $range[$codepoint] = '2c60_2c7f'; // Latin Extended-C
  350. } elseif ($codepoint < 11520) {
  351. $range[$codepoint] = '2c80_2cff'; // Coptic
  352. } elseif ($codepoint >= 65280 && $codepoint < 65520) {
  353. $range[$codepoint] = 'ff00_ffef'; // Halfwidth and Fullwidth Forms
  354. } else {
  355. $range[$codepoint] = false;
  356. }
  357. if ($range[$codepoint] === false) {
  358. return null;
  359. }
  360. if (!isset($config[$range[$codepoint]])) {
  361. $file = dirname(__FILE__) . '/multibyte_string_database/casefolding/' . $range[$codepoint] . '.php';
  362. if (file_exists($file)) {
  363. include $file;
  364. }
  365. }
  366. }
  367. if ($range[$codepoint] === false || !isset($config[$range[$codepoint]])) {
  368. return null;
  369. }
  370. $result = array();
  371. $count = count($config[$range[$codepoint]]);
  372. for ($i = 0; $i < $count; $i++) {
  373. if ($type === 'lower' && $config[$range[$codepoint]][$i][$type][0] === $codepoint) {
  374. $result[] = $config[$range[$codepoint]][$i];
  375. } elseif ($type === 'upper' && $config[$range[$codepoint]][$i][$type] === $codepoint) {
  376. $result[] = $config[$range[$codepoint]][$i];
  377. }
  378. }
  379. return $result;
  380. }
  381. /**
  382. * ----------------------------------------------------------------------------
  383. * Appendix to "Common sting operations with arrays"
  384. * ----------------------------------------------------------------------------
  385. */
  386. // This (callback) function convers from UTF-8 to other encoding.
  387. // It works with arrays of strings too.
  388. function _api_array_utf8_decode($variable, $encoding) {
  389. if (is_array($variable)) {
  390. return array_map('_api_array_utf8_decode', $variable, $encoding);
  391. }
  392. if (is_string($var)) {
  393. return api_utf8_decode($variable, $encoding);
  394. }
  395. return $variable;
  396. }
  397. /**
  398. * ----------------------------------------------------------------------------
  399. * Appendix to "String comparison"
  400. * ----------------------------------------------------------------------------
  401. */
  402. // Returns an instance of Collator class (ICU) created for a specified language.
  403. function _api_get_collator($language = null) {
  404. static $collator = array();
  405. if (!isset($collator[$language])) {
  406. $locale = api_get_locale_from_language($language);
  407. $collator[$language] = collator_create($locale);
  408. if (is_object($collator[$language])) {
  409. collator_set_attribute($collator[$language], Collator::CASE_FIRST, Collator::UPPER_FIRST);
  410. }
  411. }
  412. return $collator[$language];
  413. }
  414. // Returns an instance of Collator class (ICU) created for a specified language.
  415. // This collator treats substrings of digits as numbers.
  416. function _api_get_alpha_numerical_collator($language = null) {
  417. static $collator = array();
  418. if (!isset($collator[$language])) {
  419. $locale = api_get_locale_from_language($language);
  420. $collator[$language] = collator_create($locale);
  421. if (is_object($collator[$language])) {
  422. collator_set_attribute($collator[$language], Collator::CASE_FIRST, Collator::UPPER_FIRST);
  423. collator_set_attribute($collator[$language], Collator::NUMERIC_COLLATION, Collator::ON);
  424. }
  425. }
  426. return $collator[$language];
  427. }
  428. // Global variables used by the sorting functions.
  429. $_api_collator = null;
  430. $_api_encoding = null;
  431. // A string comparison function that serves sorting functions.
  432. function _api_cmp($string1, $string2) {
  433. global $_api_collator, $_api_encoding;
  434. $result = collator_compare($_api_collator, api_utf8_encode($string1, $_api_encoding), api_utf8_encode($string2, $_api_encoding));
  435. return $result === false ? 0 : $result;
  436. }
  437. // A reverse string comparison function that serves sorting functions.
  438. function _api_rcmp($string1, $string2) {
  439. global $_api_collator, $_api_encoding;
  440. $result = collator_compare($_api_collator, api_utf8_encode($string2, $_api_encoding), api_utf8_encode($string1, $_api_encoding));
  441. return $result === false ? 0 : $result;
  442. }
  443. // A case-insensitive string comparison function that serves sorting functions.
  444. function _api_casecmp($string1, $string2) {
  445. global $_api_collator, $_api_encoding;
  446. $result = collator_compare($_api_collator, api_strtolower(api_utf8_encode($string1, $_api_encoding), 'UTF-8'), api_strtolower(api_utf8_encode($string2, $_api_encoding), 'UTF-8'));
  447. return $result === false ? 0 : $result;
  448. }
  449. // A reverse case-insensitive string comparison function that serves sorting functions.
  450. function _api_casercmp($string1, $string2) {
  451. global $_api_collator, $_api_encoding;
  452. $result = collator_compare($_api_collator, api_strtolower(api_utf8_encode($string2, $_api_encoding), 'UTF-8'), api_strtolower(api_utf8_encode($string1, $_api_encoding), 'UTF-8'));
  453. return $result === false ? 0 : $result;
  454. }
  455. // A reverse function from strnatcmp().
  456. function _api_strnatrcmp($string1, $string2) {
  457. return strnatcmp($string2, $string1);
  458. }
  459. // A reverse function from strnatcasecmp().
  460. function _api_strnatcasercmp($string1, $string2) {
  461. return strnatcasecmp($string2, $string1);
  462. }
  463. // A fuction that translates sorting flag constants from php core to correspondent constants from intl extension.
  464. function _api_get_collator_sort_flag($sort_flag = SORT_REGULAR) {
  465. switch ($sort_flag) {
  466. case SORT_STRING:
  467. case SORT_SORT_LOCALE_STRING:
  468. return Collator::SORT_STRING;
  469. case SORT_NUMERIC:
  470. return Collator::SORT_NUMERIC;
  471. }
  472. return Collator::SORT_REGULAR;
  473. }
  474. /**
  475. * ----------------------------------------------------------------------------
  476. * Appendix to "Encoding management functions"
  477. * ----------------------------------------------------------------------------
  478. */
  479. // Ckecks whether a given encoding defines single-byte characters.
  480. // The result might be not accurate for unknown by this library encodings.
  481. function _api_is_single_byte_encoding($encoding) {
  482. static $checked = array();
  483. if (!isset($checked[$encoding])) {
  484. $character_map = _api_get_character_map_name(api_refine_encoding_id($encoding));
  485. $checked[$encoding] = (!empty($character_map) && $character_map != 'UTF-8');
  486. }
  487. return $checked[$encoding];
  488. }
  489. // This function checks whether the function _api_convert_encoding() (the php-
  490. // implementation) is able to convert from/to a given encoding.
  491. function _api_convert_encoding_supports($encoding) {
  492. static $supports = array();
  493. if (!isset($supports[encoding])) {
  494. $supports[encoding] = _api_get_character_map_name($encoding) != '';
  495. }
  496. return $supports[encoding];
  497. }
  498. /**
  499. * ----------------------------------------------------------------------------
  500. * Upgrading the PHP5 mbstring extension
  501. * ----------------------------------------------------------------------------
  502. */
  503. // This is a multibyte replacement of strchr().
  504. // This function exists in PHP 5 >= 5.2.0
  505. // See http://php.net/manual/en/function.mb-strrchr
  506. if (MBSTRING_INSTALLED && !function_exists('mb_strchr')) {
  507. function mb_strchr($haystack, $needle, $part = false, $encoding = null) {
  508. if (empty($encoding)) {
  509. $encoding = mb_internal_encoding();
  510. }
  511. return mb_strstr($haystack, $needle, $part, $encoding);
  512. }
  513. }
  514. // This is a multibyte replacement of stripos().
  515. // This function exists in PHP 5 >= 5.2.0
  516. // See http://php.net/manual/en/function.mb-stripos
  517. if (MBSTRING_INSTALLED && !function_exists('mb_stripos')) {
  518. function mb_stripos($haystack, $needle, $offset = 0, $encoding = null) {
  519. if (empty($encoding)) {
  520. $encoding = mb_internal_encoding();
  521. }
  522. return mb_strpos(mb_strtolower($haystack, $encoding), mb_strtolower($needle, $encoding), $offset, $encoding);
  523. }
  524. }
  525. // This is a multibyte replacement of stristr().
  526. // This function exists in PHP 5 >= 5.2.0
  527. // See http://php.net/manual/en/function.mb-stristr
  528. if (MBSTRING_INSTALLED && !function_exists('mb_stristr')) {
  529. function mb_stristr($haystack, $needle, $part = false, $encoding = null) {
  530. if (empty($encoding)) {
  531. $encoding = mb_internal_encoding();
  532. }
  533. $pos = mb_strpos(mb_strtolower($haystack, $encoding), mb_strtolower($needle, $encoding), 0, $encoding);
  534. if ($pos === false) {
  535. return false;
  536. }
  537. elseif($part == true) {
  538. return mb_substr($haystack, 0, $pos + 1, $encoding);
  539. } else {
  540. return mb_substr($haystack, $pos, mb_strlen($haystack, $encoding), $encoding);
  541. }
  542. }
  543. }
  544. // This is a multibyte replacement of strrchr().
  545. // This function exists in PHP 5 >= 5.2.0
  546. // See http://php.net/manual/en/function.mb-strrchr
  547. if (MBSTRING_INSTALLED && !function_exists('mb_strrchr')) {
  548. function mb_strrchr($haystack, $needle, $part = false, $encoding = null) {
  549. if (empty($encoding)) {
  550. $encoding = mb_internal_encoding();
  551. }
  552. $needle = mb_substr($needle, 0, 1, $encoding);
  553. $pos = mb_strrpos($haystack, $needle, mb_strlen($haystack, $encoding) - 1, $encoding);
  554. if ($pos === false) {
  555. return false;
  556. } elseif($part == true) {
  557. return mb_substr($haystack, 0, $pos + 1, $encoding);
  558. } else {
  559. return mb_substr($haystack, $pos, mb_strlen($haystack, $encoding), $encoding);
  560. }
  561. }
  562. }
  563. // This is a multibyte replacement of strstr().
  564. // This function exists in PHP 5 >= 5.2.0
  565. // See http://php.net/manual/en/function.mb-strstr
  566. if (MBSTRING_INSTALLED && !function_exists('mb_strstr')) {
  567. function mb_strstr($haystack, $needle, $part = false, $encoding = null) {
  568. if (empty($encoding)) {
  569. $encoding = mb_internal_encoding();
  570. }
  571. $pos = mb_strpos($haystack, $needle, 0, $encoding);
  572. if ($pos === false) {
  573. return false;
  574. } elseif($part == true) {
  575. return mb_substr($haystack, 0, $pos + 1, $encoding);
  576. } else {
  577. return mb_substr($haystack, $pos, mb_strlen($haystack, $encoding), $encoding);
  578. }
  579. }
  580. }