Mbstring.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  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. * - mb_ucfirst - Make a string's first character uppercase
  49. * - mb_lcfirst - Make a string's first character lowercase
  50. *
  51. * Not implemented:
  52. * - mb_convert_kana - Convert "kana" one from another ("zen-kaku", "han-kaku" and more)
  53. * - mb_ereg_* - Regular expression with multibyte support
  54. * - mb_parse_str - Parse GET/POST/COOKIE data and set global variable
  55. * - mb_preferred_mime_name - Get MIME charset string
  56. * - mb_regex_encoding - Returns current encoding for multibyte regex as string
  57. * - mb_regex_set_options - Set/Get the default options for mbregex functions
  58. * - mb_send_mail - Send encoded mail
  59. * - mb_split - Split multibyte string using regular expression
  60. * - mb_strcut - Get part of string
  61. * - mb_strimwidth - Get truncated string with specified width
  62. *
  63. * @author Nicolas Grekas <p@tchwork.com>
  64. *
  65. * @internal
  66. */
  67. final class Mbstring
  68. {
  69. public const MB_CASE_FOLD = \PHP_INT_MAX;
  70. private const SIMPLE_CASE_FOLD = [
  71. ['µ', 'ſ', "\xCD\x85", 'ς', "\xCF\x90", "\xCF\x91", "\xCF\x95", "\xCF\x96", "\xCF\xB0", "\xCF\xB1", "\xCF\xB5", "\xE1\xBA\x9B", "\xE1\xBE\xBE"],
  72. ['μ', 's', 'ι', 'σ', 'β', 'θ', 'φ', 'π', 'κ', 'ρ', 'ε', "\xE1\xB9\xA1", 'ι'],
  73. ];
  74. private static $encodingList = ['ASCII', 'UTF-8'];
  75. private static $language = 'neutral';
  76. private static $internalEncoding = 'UTF-8';
  77. public static function mb_convert_encoding($s, $toEncoding, $fromEncoding = null)
  78. {
  79. if (\is_array($s)) {
  80. if (PHP_VERSION_ID < 70200) {
  81. trigger_error('mb_convert_encoding() expects parameter 1 to be string, array given', \E_USER_WARNING);
  82. return null;
  83. }
  84. $r = [];
  85. foreach ($s as $str) {
  86. $r[] = self::mb_convert_encoding($str, $toEncoding, $fromEncoding);
  87. }
  88. return $r;
  89. }
  90. if (\is_array($fromEncoding) || (null !== $fromEncoding && false !== strpos($fromEncoding, ','))) {
  91. $fromEncoding = self::mb_detect_encoding($s, $fromEncoding);
  92. } else {
  93. $fromEncoding = self::getEncoding($fromEncoding);
  94. }
  95. $toEncoding = self::getEncoding($toEncoding);
  96. if ('BASE64' === $fromEncoding) {
  97. $s = base64_decode($s);
  98. $fromEncoding = $toEncoding;
  99. }
  100. if ('BASE64' === $toEncoding) {
  101. return base64_encode($s);
  102. }
  103. if ('HTML-ENTITIES' === $toEncoding || 'HTML' === $toEncoding) {
  104. if ('HTML-ENTITIES' === $fromEncoding || 'HTML' === $fromEncoding) {
  105. $fromEncoding = 'Windows-1252';
  106. }
  107. if ('UTF-8' !== $fromEncoding) {
  108. $s = iconv($fromEncoding, 'UTF-8//IGNORE', $s);
  109. }
  110. return preg_replace_callback('/[\x80-\xFF]+/', [__CLASS__, 'html_encoding_callback'], $s);
  111. }
  112. if ('HTML-ENTITIES' === $fromEncoding) {
  113. $s = html_entity_decode($s, \ENT_COMPAT, 'UTF-8');
  114. $fromEncoding = 'UTF-8';
  115. }
  116. return iconv($fromEncoding, $toEncoding.'//IGNORE', $s);
  117. }
  118. public static function mb_convert_variables($toEncoding, $fromEncoding, &...$vars)
  119. {
  120. $ok = true;
  121. array_walk_recursive($vars, function (&$v) use (&$ok, $toEncoding, $fromEncoding) {
  122. if (false === $v = self::mb_convert_encoding($v, $toEncoding, $fromEncoding)) {
  123. $ok = false;
  124. }
  125. });
  126. return $ok ? $fromEncoding : false;
  127. }
  128. public static function mb_decode_mimeheader($s)
  129. {
  130. return iconv_mime_decode($s, 2, self::$internalEncoding);
  131. }
  132. public static function mb_encode_mimeheader($s, $charset = null, $transferEncoding = null, $linefeed = null, $indent = null)
  133. {
  134. trigger_error('mb_encode_mimeheader() is bugged. Please use iconv_mime_encode() instead', \E_USER_WARNING);
  135. }
  136. public static function mb_decode_numericentity($s, $convmap, $encoding = null)
  137. {
  138. if (null !== $s && !\is_scalar($s) && !(\is_object($s) && method_exists($s, '__toString'))) {
  139. trigger_error('mb_decode_numericentity() expects parameter 1 to be string, '.\gettype($s).' given', \E_USER_WARNING);
  140. return null;
  141. }
  142. if (!\is_array($convmap) || (80000 > \PHP_VERSION_ID && !$convmap)) {
  143. return false;
  144. }
  145. if (null !== $encoding && !\is_scalar($encoding)) {
  146. trigger_error('mb_decode_numericentity() expects parameter 3 to be string, '.\gettype($s).' given', \E_USER_WARNING);
  147. return ''; // Instead of null (cf. mb_encode_numericentity).
  148. }
  149. $s = (string) $s;
  150. if ('' === $s) {
  151. return '';
  152. }
  153. $encoding = self::getEncoding($encoding);
  154. if ('UTF-8' === $encoding) {
  155. $encoding = null;
  156. if (!preg_match('//u', $s)) {
  157. $s = @iconv('UTF-8', 'UTF-8//IGNORE', $s);
  158. }
  159. } else {
  160. $s = iconv($encoding, 'UTF-8//IGNORE', $s);
  161. }
  162. $cnt = floor(\count($convmap) / 4) * 4;
  163. for ($i = 0; $i < $cnt; $i += 4) {
  164. // collector_decode_htmlnumericentity ignores $convmap[$i + 3]
  165. $convmap[$i] += $convmap[$i + 2];
  166. $convmap[$i + 1] += $convmap[$i + 2];
  167. }
  168. $s = preg_replace_callback('/&#(?:0*([0-9]+)|x0*([0-9a-fA-F]+))(?!&);?/', function (array $m) use ($cnt, $convmap) {
  169. $c = isset($m[2]) ? (int) hexdec($m[2]) : $m[1];
  170. for ($i = 0; $i < $cnt; $i += 4) {
  171. if ($c >= $convmap[$i] && $c <= $convmap[$i + 1]) {
  172. return self::mb_chr($c - $convmap[$i + 2]);
  173. }
  174. }
  175. return $m[0];
  176. }, $s);
  177. if (null === $encoding) {
  178. return $s;
  179. }
  180. return iconv('UTF-8', $encoding.'//IGNORE', $s);
  181. }
  182. public static function mb_encode_numericentity($s, $convmap, $encoding = null, $is_hex = false)
  183. {
  184. if (null !== $s && !\is_scalar($s) && !(\is_object($s) && method_exists($s, '__toString'))) {
  185. trigger_error('mb_encode_numericentity() expects parameter 1 to be string, '.\gettype($s).' given', \E_USER_WARNING);
  186. return null;
  187. }
  188. if (!\is_array($convmap) || (80000 > \PHP_VERSION_ID && !$convmap)) {
  189. return false;
  190. }
  191. if (null !== $encoding && !\is_scalar($encoding)) {
  192. trigger_error('mb_encode_numericentity() expects parameter 3 to be string, '.\gettype($s).' given', \E_USER_WARNING);
  193. return null; // Instead of '' (cf. mb_decode_numericentity).
  194. }
  195. if (null !== $is_hex && !\is_scalar($is_hex)) {
  196. trigger_error('mb_encode_numericentity() expects parameter 4 to be boolean, '.\gettype($s).' given', \E_USER_WARNING);
  197. return null;
  198. }
  199. $s = (string) $s;
  200. if ('' === $s) {
  201. return '';
  202. }
  203. $encoding = self::getEncoding($encoding);
  204. if ('UTF-8' === $encoding) {
  205. $encoding = null;
  206. if (!preg_match('//u', $s)) {
  207. $s = @iconv('UTF-8', 'UTF-8//IGNORE', $s);
  208. }
  209. } else {
  210. $s = iconv($encoding, 'UTF-8//IGNORE', $s);
  211. }
  212. static $ulenMask = ["\xC0" => 2, "\xD0" => 2, "\xE0" => 3, "\xF0" => 4];
  213. $cnt = floor(\count($convmap) / 4) * 4;
  214. $i = 0;
  215. $len = \strlen($s);
  216. $result = '';
  217. while ($i < $len) {
  218. $ulen = $s[$i] < "\x80" ? 1 : $ulenMask[$s[$i] & "\xF0"];
  219. $uchr = substr($s, $i, $ulen);
  220. $i += $ulen;
  221. $c = self::mb_ord($uchr);
  222. for ($j = 0; $j < $cnt; $j += 4) {
  223. if ($c >= $convmap[$j] && $c <= $convmap[$j + 1]) {
  224. $cOffset = ($c + $convmap[$j + 2]) & $convmap[$j + 3];
  225. $result .= $is_hex ? sprintf('&#x%X;', $cOffset) : '&#'.$cOffset.';';
  226. continue 2;
  227. }
  228. }
  229. $result .= $uchr;
  230. }
  231. if (null === $encoding) {
  232. return $result;
  233. }
  234. return iconv('UTF-8', $encoding.'//IGNORE', $result);
  235. }
  236. public static function mb_convert_case($s, $mode, $encoding = null)
  237. {
  238. $s = (string) $s;
  239. if ('' === $s) {
  240. return '';
  241. }
  242. $encoding = self::getEncoding($encoding);
  243. if ('UTF-8' === $encoding) {
  244. $encoding = null;
  245. if (!preg_match('//u', $s)) {
  246. $s = @iconv('UTF-8', 'UTF-8//IGNORE', $s);
  247. }
  248. } else {
  249. $s = iconv($encoding, 'UTF-8//IGNORE', $s);
  250. }
  251. if (\MB_CASE_TITLE == $mode) {
  252. static $titleRegexp = null;
  253. if (null === $titleRegexp) {
  254. $titleRegexp = self::getData('titleCaseRegexp');
  255. }
  256. $s = preg_replace_callback($titleRegexp, [__CLASS__, 'title_case'], $s);
  257. } else {
  258. if (\MB_CASE_UPPER == $mode) {
  259. static $upper = null;
  260. if (null === $upper) {
  261. $upper = self::getData('upperCase');
  262. }
  263. $map = $upper;
  264. } else {
  265. if (self::MB_CASE_FOLD === $mode) {
  266. static $caseFolding = null;
  267. if (null === $caseFolding) {
  268. $caseFolding = self::getData('caseFolding');
  269. }
  270. $s = strtr($s, $caseFolding);
  271. }
  272. static $lower = null;
  273. if (null === $lower) {
  274. $lower = self::getData('lowerCase');
  275. }
  276. $map = $lower;
  277. }
  278. static $ulenMask = ["\xC0" => 2, "\xD0" => 2, "\xE0" => 3, "\xF0" => 4];
  279. $i = 0;
  280. $len = \strlen($s);
  281. while ($i < $len) {
  282. $ulen = $s[$i] < "\x80" ? 1 : $ulenMask[$s[$i] & "\xF0"];
  283. $uchr = substr($s, $i, $ulen);
  284. $i += $ulen;
  285. if (isset($map[$uchr])) {
  286. $uchr = $map[$uchr];
  287. $nlen = \strlen($uchr);
  288. if ($nlen == $ulen) {
  289. $nlen = $i;
  290. do {
  291. $s[--$nlen] = $uchr[--$ulen];
  292. } while ($ulen);
  293. } else {
  294. $s = substr_replace($s, $uchr, $i - $ulen, $ulen);
  295. $len += $nlen - $ulen;
  296. $i += $nlen - $ulen;
  297. }
  298. }
  299. }
  300. }
  301. if (null === $encoding) {
  302. return $s;
  303. }
  304. return iconv('UTF-8', $encoding.'//IGNORE', $s);
  305. }
  306. public static function mb_internal_encoding($encoding = null)
  307. {
  308. if (null === $encoding) {
  309. return self::$internalEncoding;
  310. }
  311. $normalizedEncoding = self::getEncoding($encoding);
  312. if ('UTF-8' === $normalizedEncoding || false !== @iconv($normalizedEncoding, $normalizedEncoding, ' ')) {
  313. self::$internalEncoding = $normalizedEncoding;
  314. return true;
  315. }
  316. if (80000 > \PHP_VERSION_ID) {
  317. return false;
  318. }
  319. throw new \ValueError(sprintf('Argument #1 ($encoding) must be a valid encoding, "%s" given', $encoding));
  320. }
  321. public static function mb_language($lang = null)
  322. {
  323. if (null === $lang) {
  324. return self::$language;
  325. }
  326. switch ($normalizedLang = strtolower($lang)) {
  327. case 'uni':
  328. case 'neutral':
  329. self::$language = $normalizedLang;
  330. return true;
  331. }
  332. if (80000 > \PHP_VERSION_ID) {
  333. return false;
  334. }
  335. throw new \ValueError(sprintf('Argument #1 ($language) must be a valid language, "%s" given', $lang));
  336. }
  337. public static function mb_list_encodings()
  338. {
  339. return ['UTF-8'];
  340. }
  341. public static function mb_encoding_aliases($encoding)
  342. {
  343. switch (strtoupper($encoding)) {
  344. case 'UTF8':
  345. case 'UTF-8':
  346. return ['utf8'];
  347. }
  348. return false;
  349. }
  350. public static function mb_check_encoding($var = null, $encoding = null)
  351. {
  352. if (\PHP_VERSION_ID < 70200 && \is_array($var)) {
  353. trigger_error('mb_check_encoding() expects parameter 1 to be string, array given', \E_USER_WARNING);
  354. return null;
  355. }
  356. if (null === $encoding) {
  357. if (null === $var) {
  358. return false;
  359. }
  360. $encoding = self::$internalEncoding;
  361. }
  362. if (!\is_array($var)) {
  363. return self::mb_detect_encoding($var, [$encoding]) || false !== @iconv($encoding, $encoding, $var);
  364. }
  365. foreach ($var as $key => $value) {
  366. if (!self::mb_check_encoding($key, $encoding)) {
  367. return false;
  368. }
  369. if (!self::mb_check_encoding($value, $encoding)) {
  370. return false;
  371. }
  372. }
  373. return true;
  374. }
  375. public static function mb_detect_encoding($str, $encodingList = null, $strict = false)
  376. {
  377. if (null === $encodingList) {
  378. $encodingList = self::$encodingList;
  379. } else {
  380. if (!\is_array($encodingList)) {
  381. $encodingList = array_map('trim', explode(',', $encodingList));
  382. }
  383. $encodingList = array_map('strtoupper', $encodingList);
  384. }
  385. foreach ($encodingList as $enc) {
  386. switch ($enc) {
  387. case 'ASCII':
  388. if (!preg_match('/[\x80-\xFF]/', $str)) {
  389. return $enc;
  390. }
  391. break;
  392. case 'UTF8':
  393. case 'UTF-8':
  394. if (preg_match('//u', $str)) {
  395. return 'UTF-8';
  396. }
  397. break;
  398. default:
  399. if (0 === strncmp($enc, 'ISO-8859-', 9)) {
  400. return $enc;
  401. }
  402. }
  403. }
  404. return false;
  405. }
  406. public static function mb_detect_order($encodingList = null)
  407. {
  408. if (null === $encodingList) {
  409. return self::$encodingList;
  410. }
  411. if (!\is_array($encodingList)) {
  412. $encodingList = array_map('trim', explode(',', $encodingList));
  413. }
  414. $encodingList = array_map('strtoupper', $encodingList);
  415. foreach ($encodingList as $enc) {
  416. switch ($enc) {
  417. default:
  418. if (strncmp($enc, 'ISO-8859-', 9)) {
  419. return false;
  420. }
  421. // no break
  422. case 'ASCII':
  423. case 'UTF8':
  424. case 'UTF-8':
  425. }
  426. }
  427. self::$encodingList = $encodingList;
  428. return true;
  429. }
  430. public static function mb_strlen($s, $encoding = null)
  431. {
  432. $encoding = self::getEncoding($encoding);
  433. if ('CP850' === $encoding || 'ASCII' === $encoding) {
  434. return \strlen($s);
  435. }
  436. return @iconv_strlen($s, $encoding);
  437. }
  438. public static function mb_strpos($haystack, $needle, $offset = 0, $encoding = null)
  439. {
  440. $encoding = self::getEncoding($encoding);
  441. if ('CP850' === $encoding || 'ASCII' === $encoding) {
  442. return strpos($haystack, $needle, $offset);
  443. }
  444. $needle = (string) $needle;
  445. if ('' === $needle) {
  446. if (80000 > \PHP_VERSION_ID) {
  447. trigger_error(__METHOD__.': Empty delimiter', \E_USER_WARNING);
  448. return false;
  449. }
  450. return 0;
  451. }
  452. return iconv_strpos($haystack, $needle, $offset, $encoding);
  453. }
  454. public static function mb_strrpos($haystack, $needle, $offset = 0, $encoding = null)
  455. {
  456. $encoding = self::getEncoding($encoding);
  457. if ('CP850' === $encoding || 'ASCII' === $encoding) {
  458. return strrpos($haystack, $needle, $offset);
  459. }
  460. if ($offset != (int) $offset) {
  461. $offset = 0;
  462. } elseif ($offset = (int) $offset) {
  463. if ($offset < 0) {
  464. if (0 > $offset += self::mb_strlen($needle)) {
  465. $haystack = self::mb_substr($haystack, 0, $offset, $encoding);
  466. }
  467. $offset = 0;
  468. } else {
  469. $haystack = self::mb_substr($haystack, $offset, 2147483647, $encoding);
  470. }
  471. }
  472. $pos = '' !== $needle || 80000 > \PHP_VERSION_ID
  473. ? iconv_strrpos($haystack, $needle, $encoding)
  474. : self::mb_strlen($haystack, $encoding);
  475. return false !== $pos ? $offset + $pos : false;
  476. }
  477. public static function mb_str_split($string, $split_length = 1, $encoding = null)
  478. {
  479. if (null !== $string && !\is_scalar($string) && !(\is_object($string) && method_exists($string, '__toString'))) {
  480. trigger_error('mb_str_split() expects parameter 1 to be string, '.\gettype($string).' given', \E_USER_WARNING);
  481. return null;
  482. }
  483. if (1 > $split_length = (int) $split_length) {
  484. if (80000 > \PHP_VERSION_ID) {
  485. trigger_error('The length of each segment must be greater than zero', \E_USER_WARNING);
  486. return false;
  487. }
  488. throw new \ValueError('Argument #2 ($length) must be greater than 0');
  489. }
  490. if (null === $encoding) {
  491. $encoding = mb_internal_encoding();
  492. }
  493. if ('UTF-8' === $encoding = self::getEncoding($encoding)) {
  494. $rx = '/(';
  495. while (65535 < $split_length) {
  496. $rx .= '.{65535}';
  497. $split_length -= 65535;
  498. }
  499. $rx .= '.{'.$split_length.'})/us';
  500. return preg_split($rx, $string, -1, \PREG_SPLIT_DELIM_CAPTURE | \PREG_SPLIT_NO_EMPTY);
  501. }
  502. $result = [];
  503. $length = mb_strlen($string, $encoding);
  504. for ($i = 0; $i < $length; $i += $split_length) {
  505. $result[] = mb_substr($string, $i, $split_length, $encoding);
  506. }
  507. return $result;
  508. }
  509. public static function mb_strtolower($s, $encoding = null)
  510. {
  511. return self::mb_convert_case($s, \MB_CASE_LOWER, $encoding);
  512. }
  513. public static function mb_strtoupper($s, $encoding = null)
  514. {
  515. return self::mb_convert_case($s, \MB_CASE_UPPER, $encoding);
  516. }
  517. public static function mb_substitute_character($c = null)
  518. {
  519. if (null === $c) {
  520. return 'none';
  521. }
  522. if (0 === strcasecmp($c, 'none')) {
  523. return true;
  524. }
  525. if (80000 > \PHP_VERSION_ID) {
  526. return false;
  527. }
  528. if (\is_int($c) || 'long' === $c || 'entity' === $c) {
  529. return false;
  530. }
  531. throw new \ValueError('Argument #1 ($substitute_character) must be "none", "long", "entity" or a valid codepoint');
  532. }
  533. public static function mb_substr($s, $start, $length = null, $encoding = null)
  534. {
  535. $encoding = self::getEncoding($encoding);
  536. if ('CP850' === $encoding || 'ASCII' === $encoding) {
  537. return (string) substr($s, $start, null === $length ? 2147483647 : $length);
  538. }
  539. if ($start < 0) {
  540. $start = iconv_strlen($s, $encoding) + $start;
  541. if ($start < 0) {
  542. $start = 0;
  543. }
  544. }
  545. if (null === $length) {
  546. $length = 2147483647;
  547. } elseif ($length < 0) {
  548. $length = iconv_strlen($s, $encoding) + $length - $start;
  549. if ($length < 0) {
  550. return '';
  551. }
  552. }
  553. return (string) iconv_substr($s, $start, $length, $encoding);
  554. }
  555. public static function mb_stripos($haystack, $needle, $offset = 0, $encoding = null)
  556. {
  557. [$haystack, $needle] = str_replace(self::SIMPLE_CASE_FOLD[0], self::SIMPLE_CASE_FOLD[1], [
  558. self::mb_convert_case($haystack, \MB_CASE_LOWER, $encoding),
  559. self::mb_convert_case($needle, \MB_CASE_LOWER, $encoding),
  560. ]);
  561. return self::mb_strpos($haystack, $needle, $offset, $encoding);
  562. }
  563. public static function mb_stristr($haystack, $needle, $part = false, $encoding = null)
  564. {
  565. $pos = self::mb_stripos($haystack, $needle, 0, $encoding);
  566. return self::getSubpart($pos, $part, $haystack, $encoding);
  567. }
  568. public static function mb_strrchr($haystack, $needle, $part = false, $encoding = null)
  569. {
  570. $encoding = self::getEncoding($encoding);
  571. if ('CP850' === $encoding || 'ASCII' === $encoding) {
  572. $pos = strrpos($haystack, $needle);
  573. } else {
  574. $needle = self::mb_substr($needle, 0, 1, $encoding);
  575. $pos = iconv_strrpos($haystack, $needle, $encoding);
  576. }
  577. return self::getSubpart($pos, $part, $haystack, $encoding);
  578. }
  579. public static function mb_strrichr($haystack, $needle, $part = false, $encoding = null)
  580. {
  581. $needle = self::mb_substr($needle, 0, 1, $encoding);
  582. $pos = self::mb_strripos($haystack, $needle, $encoding);
  583. return self::getSubpart($pos, $part, $haystack, $encoding);
  584. }
  585. public static function mb_strripos($haystack, $needle, $offset = 0, $encoding = null)
  586. {
  587. $haystack = self::mb_convert_case($haystack, \MB_CASE_LOWER, $encoding);
  588. $needle = self::mb_convert_case($needle, \MB_CASE_LOWER, $encoding);
  589. $haystack = str_replace(self::SIMPLE_CASE_FOLD[0], self::SIMPLE_CASE_FOLD[1], $haystack);
  590. $needle = str_replace(self::SIMPLE_CASE_FOLD[0], self::SIMPLE_CASE_FOLD[1], $needle);
  591. return self::mb_strrpos($haystack, $needle, $offset, $encoding);
  592. }
  593. public static function mb_strstr($haystack, $needle, $part = false, $encoding = null)
  594. {
  595. $pos = strpos($haystack, $needle);
  596. if (false === $pos) {
  597. return false;
  598. }
  599. if ($part) {
  600. return substr($haystack, 0, $pos);
  601. }
  602. return substr($haystack, $pos);
  603. }
  604. public static function mb_get_info($type = 'all')
  605. {
  606. $info = [
  607. 'internal_encoding' => self::$internalEncoding,
  608. 'http_output' => 'pass',
  609. 'http_output_conv_mimetypes' => '^(text/|application/xhtml\+xml)',
  610. 'func_overload' => 0,
  611. 'func_overload_list' => 'no overload',
  612. 'mail_charset' => 'UTF-8',
  613. 'mail_header_encoding' => 'BASE64',
  614. 'mail_body_encoding' => 'BASE64',
  615. 'illegal_chars' => 0,
  616. 'encoding_translation' => 'Off',
  617. 'language' => self::$language,
  618. 'detect_order' => self::$encodingList,
  619. 'substitute_character' => 'none',
  620. 'strict_detection' => 'Off',
  621. ];
  622. if ('all' === $type) {
  623. return $info;
  624. }
  625. if (isset($info[$type])) {
  626. return $info[$type];
  627. }
  628. return false;
  629. }
  630. public static function mb_http_input($type = '')
  631. {
  632. return false;
  633. }
  634. public static function mb_http_output($encoding = null)
  635. {
  636. return null !== $encoding ? 'pass' === $encoding : 'pass';
  637. }
  638. public static function mb_strwidth($s, $encoding = null)
  639. {
  640. $encoding = self::getEncoding($encoding);
  641. if ('UTF-8' !== $encoding) {
  642. $s = iconv($encoding, 'UTF-8//IGNORE', $s);
  643. }
  644. $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);
  645. return ($wide << 1) + iconv_strlen($s, 'UTF-8');
  646. }
  647. public static function mb_substr_count($haystack, $needle, $encoding = null)
  648. {
  649. return substr_count($haystack, $needle);
  650. }
  651. public static function mb_output_handler($contents, $status)
  652. {
  653. return $contents;
  654. }
  655. public static function mb_chr($code, $encoding = null)
  656. {
  657. if (0x80 > $code %= 0x200000) {
  658. $s = \chr($code);
  659. } elseif (0x800 > $code) {
  660. $s = \chr(0xC0 | $code >> 6).\chr(0x80 | $code & 0x3F);
  661. } elseif (0x10000 > $code) {
  662. $s = \chr(0xE0 | $code >> 12).\chr(0x80 | $code >> 6 & 0x3F).\chr(0x80 | $code & 0x3F);
  663. } else {
  664. $s = \chr(0xF0 | $code >> 18).\chr(0x80 | $code >> 12 & 0x3F).\chr(0x80 | $code >> 6 & 0x3F).\chr(0x80 | $code & 0x3F);
  665. }
  666. if ('UTF-8' !== $encoding = self::getEncoding($encoding)) {
  667. $s = mb_convert_encoding($s, $encoding, 'UTF-8');
  668. }
  669. return $s;
  670. }
  671. public static function mb_ord($s, $encoding = null)
  672. {
  673. if ('UTF-8' !== $encoding = self::getEncoding($encoding)) {
  674. $s = mb_convert_encoding($s, 'UTF-8', $encoding);
  675. }
  676. if (1 === \strlen($s)) {
  677. return \ord($s);
  678. }
  679. $code = ($s = unpack('C*', substr($s, 0, 4))) ? $s[1] : 0;
  680. if (0xF0 <= $code) {
  681. return (($code - 0xF0) << 18) + (($s[2] - 0x80) << 12) + (($s[3] - 0x80) << 6) + $s[4] - 0x80;
  682. }
  683. if (0xE0 <= $code) {
  684. return (($code - 0xE0) << 12) + (($s[2] - 0x80) << 6) + $s[3] - 0x80;
  685. }
  686. if (0xC0 <= $code) {
  687. return (($code - 0xC0) << 6) + $s[2] - 0x80;
  688. }
  689. return $code;
  690. }
  691. public static function mb_str_pad(string $string, int $length, string $pad_string = ' ', int $pad_type = \STR_PAD_RIGHT, ?string $encoding = null): string
  692. {
  693. if (!\in_array($pad_type, [\STR_PAD_RIGHT, \STR_PAD_LEFT, \STR_PAD_BOTH], true)) {
  694. throw new \ValueError('mb_str_pad(): Argument #4 ($pad_type) must be STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH');
  695. }
  696. if (null === $encoding) {
  697. $encoding = self::mb_internal_encoding();
  698. } else {
  699. self::assertEncoding($encoding, 'mb_str_pad(): Argument #5 ($encoding) must be a valid encoding, "%s" given');
  700. }
  701. if (self::mb_strlen($pad_string, $encoding) <= 0) {
  702. throw new \ValueError('mb_str_pad(): Argument #3 ($pad_string) must be a non-empty string');
  703. }
  704. $paddingRequired = $length - self::mb_strlen($string, $encoding);
  705. if ($paddingRequired < 1) {
  706. return $string;
  707. }
  708. switch ($pad_type) {
  709. case \STR_PAD_LEFT:
  710. return self::mb_substr(str_repeat($pad_string, $paddingRequired), 0, $paddingRequired, $encoding).$string;
  711. case \STR_PAD_RIGHT:
  712. return $string.self::mb_substr(str_repeat($pad_string, $paddingRequired), 0, $paddingRequired, $encoding);
  713. default:
  714. $leftPaddingLength = floor($paddingRequired / 2);
  715. $rightPaddingLength = $paddingRequired - $leftPaddingLength;
  716. return self::mb_substr(str_repeat($pad_string, $leftPaddingLength), 0, $leftPaddingLength, $encoding).$string.self::mb_substr(str_repeat($pad_string, $rightPaddingLength), 0, $rightPaddingLength, $encoding);
  717. }
  718. }
  719. public static function mb_ucfirst(string $string, ?string $encoding = null): string
  720. {
  721. if (null === $encoding) {
  722. $encoding = self::mb_internal_encoding();
  723. } else {
  724. self::assertEncoding($encoding, 'mb_ucfirst(): Argument #2 ($encoding) must be a valid encoding, "%s" given');
  725. }
  726. $firstChar = mb_substr($string, 0, 1, $encoding);
  727. $firstChar = mb_convert_case($firstChar, \MB_CASE_TITLE, $encoding);
  728. return $firstChar.mb_substr($string, 1, null, $encoding);
  729. }
  730. public static function mb_lcfirst(string $string, ?string $encoding = null): string
  731. {
  732. if (null === $encoding) {
  733. $encoding = self::mb_internal_encoding();
  734. } else {
  735. self::assertEncoding($encoding, 'mb_lcfirst(): Argument #2 ($encoding) must be a valid encoding, "%s" given');
  736. }
  737. $firstChar = mb_substr($string, 0, 1, $encoding);
  738. $firstChar = mb_convert_case($firstChar, \MB_CASE_LOWER, $encoding);
  739. return $firstChar.mb_substr($string, 1, null, $encoding);
  740. }
  741. private static function getSubpart($pos, $part, $haystack, $encoding)
  742. {
  743. if (false === $pos) {
  744. return false;
  745. }
  746. if ($part) {
  747. return self::mb_substr($haystack, 0, $pos, $encoding);
  748. }
  749. return self::mb_substr($haystack, $pos, null, $encoding);
  750. }
  751. private static function html_encoding_callback(array $m)
  752. {
  753. $i = 1;
  754. $entities = '';
  755. $m = unpack('C*', htmlentities($m[0], \ENT_COMPAT, 'UTF-8'));
  756. while (isset($m[$i])) {
  757. if (0x80 > $m[$i]) {
  758. $entities .= \chr($m[$i++]);
  759. continue;
  760. }
  761. if (0xF0 <= $m[$i]) {
  762. $c = (($m[$i++] - 0xF0) << 18) + (($m[$i++] - 0x80) << 12) + (($m[$i++] - 0x80) << 6) + $m[$i++] - 0x80;
  763. } elseif (0xE0 <= $m[$i]) {
  764. $c = (($m[$i++] - 0xE0) << 12) + (($m[$i++] - 0x80) << 6) + $m[$i++] - 0x80;
  765. } else {
  766. $c = (($m[$i++] - 0xC0) << 6) + $m[$i++] - 0x80;
  767. }
  768. $entities .= '&#'.$c.';';
  769. }
  770. return $entities;
  771. }
  772. private static function title_case(array $s)
  773. {
  774. return self::mb_convert_case($s[1], \MB_CASE_UPPER, 'UTF-8').self::mb_convert_case($s[2], \MB_CASE_LOWER, 'UTF-8');
  775. }
  776. private static function getData($file)
  777. {
  778. if (file_exists($file = __DIR__.'/Resources/unidata/'.$file.'.php')) {
  779. return require $file;
  780. }
  781. return false;
  782. }
  783. private static function getEncoding($encoding)
  784. {
  785. if (null === $encoding) {
  786. return self::$internalEncoding;
  787. }
  788. if ('UTF-8' === $encoding) {
  789. return 'UTF-8';
  790. }
  791. $encoding = strtoupper($encoding);
  792. if ('8BIT' === $encoding || 'BINARY' === $encoding) {
  793. return 'CP850';
  794. }
  795. if ('UTF8' === $encoding) {
  796. return 'UTF-8';
  797. }
  798. return $encoding;
  799. }
  800. private static function assertEncoding(string $encoding, string $errorFormat): void
  801. {
  802. try {
  803. $validEncoding = @self::mb_check_encoding('', $encoding);
  804. } catch (\ValueError $e) {
  805. throw new \ValueError(\sprintf($errorFormat, $encoding));
  806. }
  807. // BC for PHP 7.3 and lower
  808. if (!$validEncoding) {
  809. throw new \ValueError(\sprintf($errorFormat, $encoding));
  810. }
  811. }
  812. }