Mbstring.php 36 KB

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