Mbstring.php 26 KB

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