Mbstring.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Polyfill\Mbstring;
  11. /**
  12. * Partial mbstring implementation in PHP, iconv based, UTF-8 centric.
  13. *
  14. * Implemented:
  15. * - mb_chr - Returns a specific character from its Unicode code point
  16. * - mb_convert_encoding - Convert character encoding
  17. * - mb_convert_variables - Convert character code in variable(s)
  18. * - mb_decode_mimeheader - Decode string in MIME header field
  19. * - mb_encode_mimeheader - Encode string for MIME header XXX NATIVE IMPLEMENTATION IS REALLY BUGGED
  20. * - mb_decode_numericentity - Decode HTML numeric string reference to character
  21. * - mb_encode_numericentity - Encode character to HTML numeric string reference
  22. * - mb_convert_case - Perform case folding on a string
  23. * - mb_detect_encoding - Detect character encoding
  24. * - mb_get_info - Get internal settings of mbstring
  25. * - mb_http_input - Detect HTTP input character encoding
  26. * - mb_http_output - Set/Get HTTP output character encoding
  27. * - mb_internal_encoding - Set/Get internal character encoding
  28. * - mb_list_encodings - Returns an array of all supported encodings
  29. * - mb_ord - Returns the Unicode code point of a character
  30. * - mb_output_handler - Callback function converts character encoding in output buffer
  31. * - mb_scrub - Replaces ill-formed byte sequences with substitute characters
  32. * - mb_strlen - Get string length
  33. * - mb_strpos - Find position of first occurrence of string in a string
  34. * - mb_strrpos - Find position of last occurrence of a string in a string
  35. * - mb_str_split - Convert a string to an array
  36. * - mb_strtolower - Make a string lowercase
  37. * - mb_strtoupper - Make a string uppercase
  38. * - mb_substitute_character - Set/Get substitution character
  39. * - mb_substr - Get part of string
  40. * - mb_stripos - Finds position of first occurrence of a string within another, case insensitive
  41. * - mb_stristr - Finds first occurrence of a string within another, case insensitive
  42. * - mb_strrchr - Finds the last occurrence of a character in a string within another
  43. * - mb_strrichr - Finds the last occurrence of a character in a string within another, case insensitive
  44. * - mb_strripos - Finds position of last occurrence of a string within another, case insensitive
  45. * - mb_strstr - Finds first occurrence of a string within another
  46. * - mb_strwidth - Return width of string
  47. * - mb_substr_count - Count the number of substring occurrences
  48. *
  49. * Not implemented:
  50. * - mb_convert_kana - Convert "kana" one from another ("zen-kaku", "han-kaku" and more)
  51. * - mb_ereg_* - Regular expression with multibyte support
  52. * - mb_parse_str - Parse GET/POST/COOKIE data and set global variable
  53. * - mb_preferred_mime_name - Get MIME charset string
  54. * - mb_regex_encoding - Returns current encoding for multibyte regex as string
  55. * - mb_regex_set_options - Set/Get the default options for mbregex functions
  56. * - mb_send_mail - Send encoded mail
  57. * - mb_split - Split multibyte string using regular expression
  58. * - mb_strcut - Get part of string
  59. * - mb_strimwidth - Get truncated string with specified width
  60. *
  61. * @author Nicolas Grekas <p@tchwork.com>
  62. *
  63. * @internal
  64. */
  65. final class Mbstring
  66. {
  67. const MB_CASE_FOLD = PHP_INT_MAX;
  68. private static $encodingList = array('ASCII', 'UTF-8');
  69. private static $language = 'neutral';
  70. private static $internalEncoding = 'UTF-8';
  71. private static $caseFold = array(
  72. array('µ', 'ſ', "\xCD\x85", 'ς', "\xCF\x90", "\xCF\x91", "\xCF\x95", "\xCF\x96", "\xCF\xB0", "\xCF\xB1", "\xCF\xB5", "\xE1\xBA\x9B", "\xE1\xBE\xBE"),
  73. array('μ', 's', 'ι', 'σ', 'β', 'θ', 'φ', 'π', 'κ', 'ρ', 'ε', "\xE1\xB9\xA1", 'ι'),
  74. );
  75. public static function mb_convert_encoding($s, $toEncoding, $fromEncoding = null)
  76. {
  77. if (\is_array($fromEncoding) || false !== strpos($fromEncoding, ',')) {
  78. $fromEncoding = self::mb_detect_encoding($s, $fromEncoding);
  79. } else {
  80. $fromEncoding = self::getEncoding($fromEncoding);
  81. }
  82. $toEncoding = self::getEncoding($toEncoding);
  83. if ('BASE64' === $fromEncoding) {
  84. $s = base64_decode($s);
  85. $fromEncoding = $toEncoding;
  86. }
  87. if ('BASE64' === $toEncoding) {
  88. return base64_encode($s);
  89. }
  90. if ('HTML-ENTITIES' === $toEncoding || 'HTML' === $toEncoding) {
  91. if ('HTML-ENTITIES' === $fromEncoding || 'HTML' === $fromEncoding) {
  92. $fromEncoding = 'Windows-1252';
  93. }
  94. if ('UTF-8' !== $fromEncoding) {
  95. $s = iconv($fromEncoding, 'UTF-8//IGNORE', $s);
  96. }
  97. return preg_replace_callback('/[\x80-\xFF]+/', array(__CLASS__, 'html_encoding_callback'), $s);
  98. }
  99. if ('HTML-ENTITIES' === $fromEncoding) {
  100. $s = html_entity_decode($s, ENT_COMPAT, 'UTF-8');
  101. $fromEncoding = 'UTF-8';
  102. }
  103. return iconv($fromEncoding, $toEncoding.'//IGNORE', $s);
  104. }
  105. public static function mb_convert_variables($toEncoding, $fromEncoding, &$a = null, &$b = null, &$c = null, &$d = null, &$e = null, &$f = null)
  106. {
  107. $vars = array(&$a, &$b, &$c, &$d, &$e, &$f);
  108. $ok = true;
  109. array_walk_recursive($vars, function (&$v) use (&$ok, $toEncoding, $fromEncoding) {
  110. if (false === $v = Mbstring::mb_convert_encoding($v, $toEncoding, $fromEncoding)) {
  111. $ok = false;
  112. }
  113. });
  114. return $ok ? $fromEncoding : false;
  115. }
  116. public static function mb_decode_mimeheader($s)
  117. {
  118. return iconv_mime_decode($s, 2, self::$internalEncoding);
  119. }
  120. public static function mb_encode_mimeheader($s, $charset = null, $transferEncoding = null, $linefeed = null, $indent = null)
  121. {
  122. trigger_error('mb_encode_mimeheader() is bugged. Please use iconv_mime_encode() instead', E_USER_WARNING);
  123. }
  124. public static function mb_decode_numericentity($s, $convmap, $encoding = null)
  125. {
  126. if (null !== $s && !\is_scalar($s) && !(\is_object($s) && \method_exists($s, '__toString'))) {
  127. trigger_error('mb_decode_numericentity() expects parameter 1 to be string, '.\gettype($s).' given', E_USER_WARNING);
  128. return null;
  129. }
  130. if (!\is_array($convmap) || !$convmap) {
  131. return false;
  132. }
  133. if (null !== $encoding && !\is_scalar($encoding)) {
  134. trigger_error('mb_decode_numericentity() expects parameter 3 to be string, '.\gettype($s).' given', E_USER_WARNING);
  135. return ''; // Instead of null (cf. mb_encode_numericentity).
  136. }
  137. $s = (string) $s;
  138. if ('' === $s) {
  139. return '';
  140. }
  141. $encoding = self::getEncoding($encoding);
  142. if ('UTF-8' === $encoding) {
  143. $encoding = null;
  144. if (!preg_match('//u', $s)) {
  145. $s = @iconv('UTF-8', 'UTF-8//IGNORE', $s);
  146. }
  147. } else {
  148. $s = iconv($encoding, 'UTF-8//IGNORE', $s);
  149. }
  150. $cnt = floor(\count($convmap) / 4) * 4;
  151. for ($i = 0; $i < $cnt; $i += 4) {
  152. // collector_decode_htmlnumericentity ignores $convmap[$i + 3]
  153. $convmap[$i] += $convmap[$i + 2];
  154. $convmap[$i + 1] += $convmap[$i + 2];
  155. }
  156. $s = preg_replace_callback('/&#(?:0*([0-9]+)|x0*([0-9a-fA-F]+))(?!&);?/', function (array $m) use ($cnt, $convmap) {
  157. $c = isset($m[2]) ? (int) hexdec($m[2]) : $m[1];
  158. for ($i = 0; $i < $cnt; $i += 4) {
  159. if ($c >= $convmap[$i] && $c <= $convmap[$i + 1]) {
  160. return Mbstring::mb_chr($c - $convmap[$i + 2]);
  161. }
  162. }
  163. return $m[0];
  164. }, $s);
  165. if (null === $encoding) {
  166. return $s;
  167. }
  168. return iconv('UTF-8', $encoding.'//IGNORE', $s);
  169. }
  170. public static function mb_encode_numericentity($s, $convmap, $encoding = null, $is_hex = false)
  171. {
  172. if (null !== $s && !\is_scalar($s) && !(\is_object($s) && \method_exists($s, '__toString'))) {
  173. trigger_error('mb_encode_numericentity() expects parameter 1 to be string, '.\gettype($s).' given', E_USER_WARNING);
  174. return null;
  175. }
  176. if (!\is_array($convmap) || !$convmap) {
  177. return false;
  178. }
  179. if (null !== $encoding && !\is_scalar($encoding)) {
  180. trigger_error('mb_encode_numericentity() expects parameter 3 to be string, '.\gettype($s).' given', E_USER_WARNING);
  181. return null; // Instead of '' (cf. mb_decode_numericentity).
  182. }
  183. if (null !== $is_hex && !\is_scalar($is_hex)) {
  184. trigger_error('mb_encode_numericentity() expects parameter 4 to be boolean, '.\gettype($s).' given', E_USER_WARNING);
  185. return null;
  186. }
  187. $s = (string) $s;
  188. if ('' === $s) {
  189. return '';
  190. }
  191. $encoding = self::getEncoding($encoding);
  192. if ('UTF-8' === $encoding) {
  193. $encoding = null;
  194. if (!preg_match('//u', $s)) {
  195. $s = @iconv('UTF-8', 'UTF-8//IGNORE', $s);
  196. }
  197. } else {
  198. $s = iconv($encoding, 'UTF-8//IGNORE', $s);
  199. }
  200. static $ulenMask = array("\xC0" => 2, "\xD0" => 2, "\xE0" => 3, "\xF0" => 4);
  201. $cnt = floor(\count($convmap) / 4) * 4;
  202. $i = 0;
  203. $len = \strlen($s);
  204. $result = '';
  205. while ($i < $len) {
  206. $ulen = $s[$i] < "\x80" ? 1 : $ulenMask[$s[$i] & "\xF0"];
  207. $uchr = substr($s, $i, $ulen);
  208. $i += $ulen;
  209. $c = self::mb_ord($uchr);
  210. for ($j = 0; $j < $cnt; $j += 4) {
  211. if ($c >= $convmap[$j] && $c <= $convmap[$j + 1]) {
  212. $cOffset = ($c + $convmap[$j + 2]) & $convmap[$j + 3];
  213. $result .= $is_hex ? sprintf('&#x%X;', $cOffset) : '&#'.$cOffset.';';
  214. continue 2;
  215. }
  216. }
  217. $result .= $uchr;
  218. }
  219. if (null === $encoding) {
  220. return $result;
  221. }
  222. return iconv('UTF-8', $encoding.'//IGNORE', $result);
  223. }
  224. public static function mb_convert_case($s, $mode, $encoding = null)
  225. {
  226. $s = (string) $s;
  227. if ('' === $s) {
  228. return '';
  229. }
  230. $encoding = self::getEncoding($encoding);
  231. if ('UTF-8' === $encoding) {
  232. $encoding = null;
  233. if (!preg_match('//u', $s)) {
  234. $s = @iconv('UTF-8', 'UTF-8//IGNORE', $s);
  235. }
  236. } else {
  237. $s = iconv($encoding, 'UTF-8//IGNORE', $s);
  238. }
  239. if (MB_CASE_TITLE == $mode) {
  240. static $titleRegexp = null;
  241. if (null === $titleRegexp) {
  242. $titleRegexp = self::getData('titleCaseRegexp');
  243. }
  244. $s = preg_replace_callback($titleRegexp, array(__CLASS__, 'title_case'), $s);
  245. } else {
  246. if (MB_CASE_UPPER == $mode) {
  247. static $upper = null;
  248. if (null === $upper) {
  249. $upper = self::getData('upperCase');
  250. }
  251. $map = $upper;
  252. } else {
  253. if (self::MB_CASE_FOLD === $mode) {
  254. $s = str_replace(self::$caseFold[0], self::$caseFold[1], $s);
  255. }
  256. static $lower = null;
  257. if (null === $lower) {
  258. $lower = self::getData('lowerCase');
  259. }
  260. $map = $lower;
  261. }
  262. static $ulenMask = array("\xC0" => 2, "\xD0" => 2, "\xE0" => 3, "\xF0" => 4);
  263. $i = 0;
  264. $len = \strlen($s);
  265. while ($i < $len) {
  266. $ulen = $s[$i] < "\x80" ? 1 : $ulenMask[$s[$i] & "\xF0"];
  267. $uchr = substr($s, $i, $ulen);
  268. $i += $ulen;
  269. if (isset($map[$uchr])) {
  270. $uchr = $map[$uchr];
  271. $nlen = \strlen($uchr);
  272. if ($nlen == $ulen) {
  273. $nlen = $i;
  274. do {
  275. $s[--$nlen] = $uchr[--$ulen];
  276. } while ($ulen);
  277. } else {
  278. $s = substr_replace($s, $uchr, $i - $ulen, $ulen);
  279. $len += $nlen - $ulen;
  280. $i += $nlen - $ulen;
  281. }
  282. }
  283. }
  284. }
  285. if (null === $encoding) {
  286. return $s;
  287. }
  288. return iconv('UTF-8', $encoding.'//IGNORE', $s);
  289. }
  290. public static function mb_internal_encoding($encoding = null)
  291. {
  292. if (null === $encoding) {
  293. return self::$internalEncoding;
  294. }
  295. $encoding = self::getEncoding($encoding);
  296. if ('UTF-8' === $encoding || false !== @iconv($encoding, $encoding, ' ')) {
  297. self::$internalEncoding = $encoding;
  298. return true;
  299. }
  300. return false;
  301. }
  302. public static function mb_language($lang = null)
  303. {
  304. if (null === $lang) {
  305. return self::$language;
  306. }
  307. switch ($lang = strtolower($lang)) {
  308. case 'uni':
  309. case 'neutral':
  310. self::$language = $lang;
  311. return true;
  312. }
  313. return false;
  314. }
  315. public static function mb_list_encodings()
  316. {
  317. return array('UTF-8');
  318. }
  319. public static function mb_encoding_aliases($encoding)
  320. {
  321. switch (strtoupper($encoding)) {
  322. case 'UTF8':
  323. case 'UTF-8':
  324. return array('utf8');
  325. }
  326. return false;
  327. }
  328. public static function mb_check_encoding($var = null, $encoding = null)
  329. {
  330. if (null === $encoding) {
  331. if (null === $var) {
  332. return false;
  333. }
  334. $encoding = self::$internalEncoding;
  335. }
  336. return self::mb_detect_encoding($var, array($encoding)) || false !== @iconv($encoding, $encoding, $var);
  337. }
  338. public static function mb_detect_encoding($str, $encodingList = null, $strict = false)
  339. {
  340. if (null === $encodingList) {
  341. $encodingList = self::$encodingList;
  342. } else {
  343. if (!\is_array($encodingList)) {
  344. $encodingList = array_map('trim', explode(',', $encodingList));
  345. }
  346. $encodingList = array_map('strtoupper', $encodingList);
  347. }
  348. foreach ($encodingList as $enc) {
  349. switch ($enc) {
  350. case 'ASCII':
  351. if (!preg_match('/[\x80-\xFF]/', $str)) {
  352. return $enc;
  353. }
  354. break;
  355. case 'UTF8':
  356. case 'UTF-8':
  357. if (preg_match('//u', $str)) {
  358. return 'UTF-8';
  359. }
  360. break;
  361. default:
  362. if (0 === strncmp($enc, 'ISO-8859-', 9)) {
  363. return $enc;
  364. }
  365. }
  366. }
  367. return false;
  368. }
  369. public static function mb_detect_order($encodingList = null)
  370. {
  371. if (null === $encodingList) {
  372. return self::$encodingList;
  373. }
  374. if (!\is_array($encodingList)) {
  375. $encodingList = array_map('trim', explode(',', $encodingList));
  376. }
  377. $encodingList = array_map('strtoupper', $encodingList);
  378. foreach ($encodingList as $enc) {
  379. switch ($enc) {
  380. default:
  381. if (strncmp($enc, 'ISO-8859-', 9)) {
  382. return false;
  383. }
  384. // no break
  385. case 'ASCII':
  386. case 'UTF8':
  387. case 'UTF-8':
  388. }
  389. }
  390. self::$encodingList = $encodingList;
  391. return true;
  392. }
  393. public static function mb_strlen($s, $encoding = null)
  394. {
  395. $encoding = self::getEncoding($encoding);
  396. if ('CP850' === $encoding || 'ASCII' === $encoding) {
  397. return \strlen($s);
  398. }
  399. return @iconv_strlen($s, $encoding);
  400. }
  401. public static function mb_strpos($haystack, $needle, $offset = 0, $encoding = null)
  402. {
  403. $encoding = self::getEncoding($encoding);
  404. if ('CP850' === $encoding || 'ASCII' === $encoding) {
  405. return strpos($haystack, $needle, $offset);
  406. }
  407. $needle = (string) $needle;
  408. if ('' === $needle) {
  409. trigger_error(__METHOD__.': Empty delimiter', E_USER_WARNING);
  410. return false;
  411. }
  412. return iconv_strpos($haystack, $needle, $offset, $encoding);
  413. }
  414. public static function mb_strrpos($haystack, $needle, $offset = 0, $encoding = null)
  415. {
  416. $encoding = self::getEncoding($encoding);
  417. if ('CP850' === $encoding || 'ASCII' === $encoding) {
  418. return strrpos($haystack, $needle, $offset);
  419. }
  420. if ($offset != (int) $offset) {
  421. $offset = 0;
  422. } elseif ($offset = (int) $offset) {
  423. if ($offset < 0) {
  424. $haystack = self::mb_substr($haystack, 0, $offset, $encoding);
  425. $offset = 0;
  426. } else {
  427. $haystack = self::mb_substr($haystack, $offset, 2147483647, $encoding);
  428. }
  429. }
  430. $pos = iconv_strrpos($haystack, $needle, $encoding);
  431. return false !== $pos ? $offset + $pos : false;
  432. }
  433. public static function mb_str_split($string, $split_length = 1, $encoding = null)
  434. {
  435. if (null !== $string && !\is_scalar($string) && !(\is_object($string) && \method_exists($string, '__toString'))) {
  436. trigger_error('mb_str_split() expects parameter 1 to be string, '.\gettype($string).' given', E_USER_WARNING);
  437. return null;
  438. }
  439. if ($split_length < 1) {
  440. trigger_error('The length of each segment must be greater than zero', E_USER_WARNING);
  441. return false;
  442. }
  443. if (null === $encoding) {
  444. $encoding = mb_internal_encoding();
  445. }
  446. $result = array();
  447. $length = mb_strlen($string, $encoding);
  448. for ($i = 0; $i < $length; $i += $split_length) {
  449. $result[] = mb_substr($string, $i, $split_length, $encoding);
  450. }
  451. return $result;
  452. }
  453. public static function mb_strtolower($s, $encoding = null)
  454. {
  455. return self::mb_convert_case($s, MB_CASE_LOWER, $encoding);
  456. }
  457. public static function mb_strtoupper($s, $encoding = null)
  458. {
  459. return self::mb_convert_case($s, MB_CASE_UPPER, $encoding);
  460. }
  461. public static function mb_substitute_character($c = null)
  462. {
  463. if (0 === strcasecmp($c, 'none')) {
  464. return true;
  465. }
  466. return null !== $c ? false : 'none';
  467. }
  468. public static function mb_substr($s, $start, $length = null, $encoding = null)
  469. {
  470. $encoding = self::getEncoding($encoding);
  471. if ('CP850' === $encoding || 'ASCII' === $encoding) {
  472. return (string) substr($s, $start, null === $length ? 2147483647 : $length);
  473. }
  474. if ($start < 0) {
  475. $start = iconv_strlen($s, $encoding) + $start;
  476. if ($start < 0) {
  477. $start = 0;
  478. }
  479. }
  480. if (null === $length) {
  481. $length = 2147483647;
  482. } elseif ($length < 0) {
  483. $length = iconv_strlen($s, $encoding) + $length - $start;
  484. if ($length < 0) {
  485. return '';
  486. }
  487. }
  488. return (string) iconv_substr($s, $start, $length, $encoding);
  489. }
  490. public static function mb_stripos($haystack, $needle, $offset = 0, $encoding = null)
  491. {
  492. $haystack = self::mb_convert_case($haystack, self::MB_CASE_FOLD, $encoding);
  493. $needle = self::mb_convert_case($needle, self::MB_CASE_FOLD, $encoding);
  494. return self::mb_strpos($haystack, $needle, $offset, $encoding);
  495. }
  496. public static function mb_stristr($haystack, $needle, $part = false, $encoding = null)
  497. {
  498. $pos = self::mb_stripos($haystack, $needle, 0, $encoding);
  499. return self::getSubpart($pos, $part, $haystack, $encoding);
  500. }
  501. public static function mb_strrchr($haystack, $needle, $part = false, $encoding = null)
  502. {
  503. $encoding = self::getEncoding($encoding);
  504. if ('CP850' === $encoding || 'ASCII' === $encoding) {
  505. return strrchr($haystack, $needle, $part);
  506. }
  507. $needle = self::mb_substr($needle, 0, 1, $encoding);
  508. $pos = iconv_strrpos($haystack, $needle, $encoding);
  509. return self::getSubpart($pos, $part, $haystack, $encoding);
  510. }
  511. public static function mb_strrichr($haystack, $needle, $part = false, $encoding = null)
  512. {
  513. $needle = self::mb_substr($needle, 0, 1, $encoding);
  514. $pos = self::mb_strripos($haystack, $needle, $encoding);
  515. return self::getSubpart($pos, $part, $haystack, $encoding);
  516. }
  517. public static function mb_strripos($haystack, $needle, $offset = 0, $encoding = null)
  518. {
  519. $haystack = self::mb_convert_case($haystack, self::MB_CASE_FOLD, $encoding);
  520. $needle = self::mb_convert_case($needle, self::MB_CASE_FOLD, $encoding);
  521. return self::mb_strrpos($haystack, $needle, $offset, $encoding);
  522. }
  523. public static function mb_strstr($haystack, $needle, $part = false, $encoding = null)
  524. {
  525. $pos = strpos($haystack, $needle);
  526. if (false === $pos) {
  527. return false;
  528. }
  529. if ($part) {
  530. return substr($haystack, 0, $pos);
  531. }
  532. return substr($haystack, $pos);
  533. }
  534. public static function mb_get_info($type = 'all')
  535. {
  536. $info = array(
  537. 'internal_encoding' => self::$internalEncoding,
  538. 'http_output' => 'pass',
  539. 'http_output_conv_mimetypes' => '^(text/|application/xhtml\+xml)',
  540. 'func_overload' => 0,
  541. 'func_overload_list' => 'no overload',
  542. 'mail_charset' => 'UTF-8',
  543. 'mail_header_encoding' => 'BASE64',
  544. 'mail_body_encoding' => 'BASE64',
  545. 'illegal_chars' => 0,
  546. 'encoding_translation' => 'Off',
  547. 'language' => self::$language,
  548. 'detect_order' => self::$encodingList,
  549. 'substitute_character' => 'none',
  550. 'strict_detection' => 'Off',
  551. );
  552. if ('all' === $type) {
  553. return $info;
  554. }
  555. if (isset($info[$type])) {
  556. return $info[$type];
  557. }
  558. return false;
  559. }
  560. public static function mb_http_input($type = '')
  561. {
  562. return false;
  563. }
  564. public static function mb_http_output($encoding = null)
  565. {
  566. return null !== $encoding ? 'pass' === $encoding : 'pass';
  567. }
  568. public static function mb_strwidth($s, $encoding = null)
  569. {
  570. $encoding = self::getEncoding($encoding);
  571. if ('UTF-8' !== $encoding) {
  572. $s = iconv($encoding, 'UTF-8//IGNORE', $s);
  573. }
  574. $s = preg_replace('/[\x{1100}-\x{115F}\x{2329}\x{232A}\x{2E80}-\x{303E}\x{3040}-\x{A4CF}\x{AC00}-\x{D7A3}\x{F900}-\x{FAFF}\x{FE10}-\x{FE19}\x{FE30}-\x{FE6F}\x{FF00}-\x{FF60}\x{FFE0}-\x{FFE6}\x{20000}-\x{2FFFD}\x{30000}-\x{3FFFD}]/u', '', $s, -1, $wide);
  575. return ($wide << 1) + iconv_strlen($s, 'UTF-8');
  576. }
  577. public static function mb_substr_count($haystack, $needle, $encoding = null)
  578. {
  579. return substr_count($haystack, $needle);
  580. }
  581. public static function mb_output_handler($contents, $status)
  582. {
  583. return $contents;
  584. }
  585. public static function mb_chr($code, $encoding = null)
  586. {
  587. if (0x80 > $code %= 0x200000) {
  588. $s = \chr($code);
  589. } elseif (0x800 > $code) {
  590. $s = \chr(0xC0 | $code >> 6).\chr(0x80 | $code & 0x3F);
  591. } elseif (0x10000 > $code) {
  592. $s = \chr(0xE0 | $code >> 12).\chr(0x80 | $code >> 6 & 0x3F).\chr(0x80 | $code & 0x3F);
  593. } else {
  594. $s = \chr(0xF0 | $code >> 18).\chr(0x80 | $code >> 12 & 0x3F).\chr(0x80 | $code >> 6 & 0x3F).\chr(0x80 | $code & 0x3F);
  595. }
  596. if ('UTF-8' !== $encoding = self::getEncoding($encoding)) {
  597. $s = mb_convert_encoding($s, $encoding, 'UTF-8');
  598. }
  599. return $s;
  600. }
  601. public static function mb_ord($s, $encoding = null)
  602. {
  603. if ('UTF-8' !== $encoding = self::getEncoding($encoding)) {
  604. $s = mb_convert_encoding($s, 'UTF-8', $encoding);
  605. }
  606. if (1 === \strlen($s)) {
  607. return \ord($s);
  608. }
  609. $code = ($s = unpack('C*', substr($s, 0, 4))) ? $s[1] : 0;
  610. if (0xF0 <= $code) {
  611. return (($code - 0xF0) << 18) + (($s[2] - 0x80) << 12) + (($s[3] - 0x80) << 6) + $s[4] - 0x80;
  612. }
  613. if (0xE0 <= $code) {
  614. return (($code - 0xE0) << 12) + (($s[2] - 0x80) << 6) + $s[3] - 0x80;
  615. }
  616. if (0xC0 <= $code) {
  617. return (($code - 0xC0) << 6) + $s[2] - 0x80;
  618. }
  619. return $code;
  620. }
  621. private static function getSubpart($pos, $part, $haystack, $encoding)
  622. {
  623. if (false === $pos) {
  624. return false;
  625. }
  626. if ($part) {
  627. return self::mb_substr($haystack, 0, $pos, $encoding);
  628. }
  629. return self::mb_substr($haystack, $pos, null, $encoding);
  630. }
  631. private static function html_encoding_callback(array $m)
  632. {
  633. $i = 1;
  634. $entities = '';
  635. $m = unpack('C*', htmlentities($m[0], ENT_COMPAT, 'UTF-8'));
  636. while (isset($m[$i])) {
  637. if (0x80 > $m[$i]) {
  638. $entities .= \chr($m[$i++]);
  639. continue;
  640. }
  641. if (0xF0 <= $m[$i]) {
  642. $c = (($m[$i++] - 0xF0) << 18) + (($m[$i++] - 0x80) << 12) + (($m[$i++] - 0x80) << 6) + $m[$i++] - 0x80;
  643. } elseif (0xE0 <= $m[$i]) {
  644. $c = (($m[$i++] - 0xE0) << 12) + (($m[$i++] - 0x80) << 6) + $m[$i++] - 0x80;
  645. } else {
  646. $c = (($m[$i++] - 0xC0) << 6) + $m[$i++] - 0x80;
  647. }
  648. $entities .= '&#'.$c.';';
  649. }
  650. return $entities;
  651. }
  652. private static function title_case(array $s)
  653. {
  654. return self::mb_convert_case($s[1], MB_CASE_UPPER, 'UTF-8').self::mb_convert_case($s[2], MB_CASE_LOWER, 'UTF-8');
  655. }
  656. private static function getData($file)
  657. {
  658. if (file_exists($file = __DIR__.'/Resources/unidata/'.$file.'.php')) {
  659. return require $file;
  660. }
  661. return false;
  662. }
  663. private static function getEncoding($encoding)
  664. {
  665. if (null === $encoding) {
  666. return self::$internalEncoding;
  667. }
  668. $encoding = strtoupper($encoding);
  669. if ('8BIT' === $encoding || 'BINARY' === $encoding) {
  670. return 'CP850';
  671. }
  672. if ('UTF8' === $encoding) {
  673. return 'UTF-8';
  674. }
  675. return $encoding;
  676. }
  677. }