Mbstring.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Polyfill\Mbstring;
  11. /**
  12. * Partial mbstring implementation in PHP, iconv based, UTF-8 centric.
  13. *
  14. * Implemented:
  15. * - mb_chr - Returns a specific character from its Unicode code point
  16. * - mb_convert_encoding - Convert character encoding
  17. * - mb_convert_variables - Convert character code in variable(s)
  18. * - mb_decode_mimeheader - Decode string in MIME header field
  19. * - mb_encode_mimeheader - Encode string for MIME header XXX NATIVE IMPLEMENTATION IS REALLY BUGGED
  20. * - mb_decode_numericentity - Decode HTML numeric string reference to character
  21. * - mb_encode_numericentity - Encode character to HTML numeric string reference
  22. * - mb_convert_case - Perform case folding on a string
  23. * - mb_detect_encoding - Detect character encoding
  24. * - mb_get_info - Get internal settings of mbstring
  25. * - mb_http_input - Detect HTTP input character encoding
  26. * - mb_http_output - Set/Get HTTP output character encoding
  27. * - mb_internal_encoding - Set/Get internal character encoding
  28. * - mb_list_encodings - Returns an array of all supported encodings
  29. * - mb_ord - Returns the Unicode code point of a character
  30. * - mb_output_handler - Callback function converts character encoding in output buffer
  31. * - mb_scrub - Replaces ill-formed byte sequences with substitute characters
  32. * - mb_strlen - Get string length
  33. * - mb_strpos - Find position of first occurrence of string in a string
  34. * - mb_strrpos - Find position of last occurrence of a string in a string
  35. * - mb_str_split - Convert a string to an array
  36. * - mb_strtolower - Make a string lowercase
  37. * - mb_strtoupper - Make a string uppercase
  38. * - mb_substitute_character - Set/Get substitution character
  39. * - mb_substr - Get part of string
  40. * - mb_stripos - Finds position of first occurrence of a string within another, case insensitive
  41. * - mb_stristr - Finds first occurrence of a string within another, case insensitive
  42. * - mb_strrchr - Finds the last occurrence of a character in a string within another
  43. * - mb_strrichr - Finds the last occurrence of a character in a string within another, case insensitive
  44. * - mb_strripos - Finds position of last occurrence of a string within another, case insensitive
  45. * - mb_strstr - Finds first occurrence of a string within another
  46. * - mb_strwidth - Return width of string
  47. * - mb_substr_count - Count the number of substring occurrences
  48. *
  49. * Not implemented:
  50. * - mb_convert_kana - Convert "kana" one from another ("zen-kaku", "han-kaku" and more)
  51. * - mb_ereg_* - Regular expression with multibyte support
  52. * - mb_parse_str - Parse GET/POST/COOKIE data and set global variable
  53. * - mb_preferred_mime_name - Get MIME charset string
  54. * - mb_regex_encoding - Returns current encoding for multibyte regex as string
  55. * - mb_regex_set_options - Set/Get the default options for mbregex functions
  56. * - mb_send_mail - Send encoded mail
  57. * - mb_split - Split multibyte string using regular expression
  58. * - mb_strcut - Get part of string
  59. * - mb_strimwidth - Get truncated string with specified width
  60. *
  61. * @author Nicolas Grekas <p@tchwork.com>
  62. *
  63. * @internal
  64. */
  65. final class Mbstring
  66. {
  67. public const MB_CASE_FOLD = \PHP_INT_MAX;
  68. private const SIMPLE_CASE_FOLD = [
  69. ['µ', 'ſ', "\xCD\x85", 'ς', "\xCF\x90", "\xCF\x91", "\xCF\x95", "\xCF\x96", "\xCF\xB0", "\xCF\xB1", "\xCF\xB5", "\xE1\xBA\x9B", "\xE1\xBE\xBE"],
  70. ['μ', 's', 'ι', 'σ', 'β', 'θ', 'φ', 'π', 'κ', 'ρ', 'ε', "\xE1\xB9\xA1", 'ι'],
  71. ];
  72. private static $encodingList = ['ASCII', 'UTF-8'];
  73. private static $language = 'neutral';
  74. private static $internalEncoding = 'UTF-8';
  75. public static function mb_convert_encoding($s, $toEncoding, $fromEncoding = null)
  76. {
  77. if (\is_array($fromEncoding) || (null !== $fromEncoding && false !== strpos($fromEncoding, ','))) {
  78. $fromEncoding = self::mb_detect_encoding($s, $fromEncoding);
  79. } else {
  80. $fromEncoding = self::getEncoding($fromEncoding);
  81. }
  82. $toEncoding = self::getEncoding($toEncoding);
  83. if ('BASE64' === $fromEncoding) {
  84. $s = base64_decode($s);
  85. $fromEncoding = $toEncoding;
  86. }
  87. if ('BASE64' === $toEncoding) {
  88. return base64_encode($s);
  89. }
  90. if ('HTML-ENTITIES' === $toEncoding || 'HTML' === $toEncoding) {
  91. if ('HTML-ENTITIES' === $fromEncoding || 'HTML' === $fromEncoding) {
  92. $fromEncoding = 'Windows-1252';
  93. }
  94. if ('UTF-8' !== $fromEncoding) {
  95. $s = iconv($fromEncoding, 'UTF-8//IGNORE', $s);
  96. }
  97. return preg_replace_callback('/[\x80-\xFF]+/', [__CLASS__, 'html_encoding_callback'], $s);
  98. }
  99. if ('HTML-ENTITIES' === $fromEncoding) {
  100. $s = html_entity_decode($s, \ENT_COMPAT, 'UTF-8');
  101. $fromEncoding = 'UTF-8';
  102. }
  103. return iconv($fromEncoding, $toEncoding.'//IGNORE', $s);
  104. }
  105. public static function mb_convert_variables($toEncoding, $fromEncoding, &...$vars)
  106. {
  107. $ok = true;
  108. array_walk_recursive($vars, function (&$v) use (&$ok, $toEncoding, $fromEncoding) {
  109. if (false === $v = self::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) || (80000 > \PHP_VERSION_ID && !$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 self::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) || (80000 > \PHP_VERSION_ID && !$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 = ["\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. static $titleRegexp = null;
  240. if (null === $titleRegexp) {
  241. $titleRegexp = self::getData('titleCaseRegexp');
  242. }
  243. $s = preg_replace_callback($titleRegexp, [__CLASS__, 'title_case'], $s);
  244. } else {
  245. if (\MB_CASE_UPPER == $mode) {
  246. static $upper = null;
  247. if (null === $upper) {
  248. $upper = self::getData('upperCase');
  249. }
  250. $map = $upper;
  251. } else {
  252. if (self::MB_CASE_FOLD === $mode) {
  253. static $caseFolding = null;
  254. if (null === $caseFolding) {
  255. $caseFolding = self::getData('caseFolding');
  256. }
  257. $s = strtr($s, $caseFolding);
  258. }
  259. static $lower = null;
  260. if (null === $lower) {
  261. $lower = self::getData('lowerCase');
  262. }
  263. $map = $lower;
  264. }
  265. static $ulenMask = ["\xC0" => 2, "\xD0" => 2, "\xE0" => 3, "\xF0" => 4];
  266. $i = 0;
  267. $len = \strlen($s);
  268. while ($i < $len) {
  269. $ulen = $s[$i] < "\x80" ? 1 : $ulenMask[$s[$i] & "\xF0"];
  270. $uchr = substr($s, $i, $ulen);
  271. $i += $ulen;
  272. if (isset($map[$uchr])) {
  273. $uchr = $map[$uchr];
  274. $nlen = \strlen($uchr);
  275. if ($nlen == $ulen) {
  276. $nlen = $i;
  277. do {
  278. $s[--$nlen] = $uchr[--$ulen];
  279. } while ($ulen);
  280. } else {
  281. $s = substr_replace($s, $uchr, $i - $ulen, $ulen);
  282. $len += $nlen - $ulen;
  283. $i += $nlen - $ulen;
  284. }
  285. }
  286. }
  287. }
  288. if (null === $encoding) {
  289. return $s;
  290. }
  291. return iconv('UTF-8', $encoding.'//IGNORE', $s);
  292. }
  293. public static function mb_internal_encoding($encoding = null)
  294. {
  295. if (null === $encoding) {
  296. return self::$internalEncoding;
  297. }
  298. $normalizedEncoding = self::getEncoding($encoding);
  299. if ('UTF-8' === $normalizedEncoding || false !== @iconv($normalizedEncoding, $normalizedEncoding, ' ')) {
  300. self::$internalEncoding = $normalizedEncoding;
  301. return true;
  302. }
  303. if (80000 > \PHP_VERSION_ID) {
  304. return false;
  305. }
  306. throw new \ValueError(sprintf('Argument #1 ($encoding) must be a valid encoding, "%s" given', $encoding));
  307. }
  308. public static function mb_language($lang = null)
  309. {
  310. if (null === $lang) {
  311. return self::$language;
  312. }
  313. switch ($normalizedLang = strtolower($lang)) {
  314. case 'uni':
  315. case 'neutral':
  316. self::$language = $normalizedLang;
  317. return true;
  318. }
  319. if (80000 > \PHP_VERSION_ID) {
  320. return false;
  321. }
  322. throw new \ValueError(sprintf('Argument #1 ($language) must be a valid language, "%s" given', $lang));
  323. }
  324. public static function mb_list_encodings()
  325. {
  326. return ['UTF-8'];
  327. }
  328. public static function mb_encoding_aliases($encoding)
  329. {
  330. switch (strtoupper($encoding)) {
  331. case 'UTF8':
  332. case 'UTF-8':
  333. return ['utf8'];
  334. }
  335. return false;
  336. }
  337. public static function mb_check_encoding($var = null, $encoding = null)
  338. {
  339. if (PHP_VERSION_ID < 70200 && \is_array($var)) {
  340. trigger_error('mb_check_encoding() expects parameter 1 to be string, array given', \E_USER_WARNING);
  341. return null;
  342. }
  343. if (null === $encoding) {
  344. if (null === $var) {
  345. return false;
  346. }
  347. $encoding = self::$internalEncoding;
  348. }
  349. if (!\is_array($var)) {
  350. return self::mb_detect_encoding($var, [$encoding]) || false !== @iconv($encoding, $encoding, $var);
  351. }
  352. foreach ($var as $key => $value) {
  353. if (!self::mb_check_encoding($key, $encoding)) {
  354. return false;
  355. }
  356. if (!self::mb_check_encoding($value, $encoding)) {
  357. return false;
  358. }
  359. }
  360. return true;
  361. }
  362. public static function mb_detect_encoding($str, $encodingList = null, $strict = false)
  363. {
  364. if (null === $encodingList) {
  365. $encodingList = self::$encodingList;
  366. } else {
  367. if (!\is_array($encodingList)) {
  368. $encodingList = array_map('trim', explode(',', $encodingList));
  369. }
  370. $encodingList = array_map('strtoupper', $encodingList);
  371. }
  372. foreach ($encodingList as $enc) {
  373. switch ($enc) {
  374. case 'ASCII':
  375. if (!preg_match('/[\x80-\xFF]/', $str)) {
  376. return $enc;
  377. }
  378. break;
  379. case 'UTF8':
  380. case 'UTF-8':
  381. if (preg_match('//u', $str)) {
  382. return 'UTF-8';
  383. }
  384. break;
  385. default:
  386. if (0 === strncmp($enc, 'ISO-8859-', 9)) {
  387. return $enc;
  388. }
  389. }
  390. }
  391. return false;
  392. }
  393. public static function mb_detect_order($encodingList = null)
  394. {
  395. if (null === $encodingList) {
  396. return self::$encodingList;
  397. }
  398. if (!\is_array($encodingList)) {
  399. $encodingList = array_map('trim', explode(',', $encodingList));
  400. }
  401. $encodingList = array_map('strtoupper', $encodingList);
  402. foreach ($encodingList as $enc) {
  403. switch ($enc) {
  404. default:
  405. if (strncmp($enc, 'ISO-8859-', 9)) {
  406. return false;
  407. }
  408. // no break
  409. case 'ASCII':
  410. case 'UTF8':
  411. case 'UTF-8':
  412. }
  413. }
  414. self::$encodingList = $encodingList;
  415. return true;
  416. }
  417. public static function mb_strlen($s, $encoding = null)
  418. {
  419. $encoding = self::getEncoding($encoding);
  420. if ('CP850' === $encoding || 'ASCII' === $encoding) {
  421. return \strlen($s);
  422. }
  423. return @iconv_strlen($s, $encoding);
  424. }
  425. public static function mb_strpos($haystack, $needle, $offset = 0, $encoding = null)
  426. {
  427. $encoding = self::getEncoding($encoding);
  428. if ('CP850' === $encoding || 'ASCII' === $encoding) {
  429. return strpos($haystack, $needle, $offset);
  430. }
  431. $needle = (string) $needle;
  432. if ('' === $needle) {
  433. if (80000 > \PHP_VERSION_ID) {
  434. trigger_error(__METHOD__.': Empty delimiter', \E_USER_WARNING);
  435. return false;
  436. }
  437. return 0;
  438. }
  439. return iconv_strpos($haystack, $needle, $offset, $encoding);
  440. }
  441. public static function mb_strrpos($haystack, $needle, $offset = 0, $encoding = null)
  442. {
  443. $encoding = self::getEncoding($encoding);
  444. if ('CP850' === $encoding || 'ASCII' === $encoding) {
  445. return strrpos($haystack, $needle, $offset);
  446. }
  447. if ($offset != (int) $offset) {
  448. $offset = 0;
  449. } elseif ($offset = (int) $offset) {
  450. if ($offset < 0) {
  451. if (0 > $offset += self::mb_strlen($needle)) {
  452. $haystack = self::mb_substr($haystack, 0, $offset, $encoding);
  453. }
  454. $offset = 0;
  455. } else {
  456. $haystack = self::mb_substr($haystack, $offset, 2147483647, $encoding);
  457. }
  458. }
  459. $pos = '' !== $needle || 80000 > \PHP_VERSION_ID
  460. ? iconv_strrpos($haystack, $needle, $encoding)
  461. : self::mb_strlen($haystack, $encoding);
  462. return false !== $pos ? $offset + $pos : false;
  463. }
  464. public static function mb_str_split($string, $split_length = 1, $encoding = null)
  465. {
  466. if (null !== $string && !\is_scalar($string) && !(\is_object($string) && method_exists($string, '__toString'))) {
  467. trigger_error('mb_str_split() expects parameter 1 to be string, '.\gettype($string).' given', \E_USER_WARNING);
  468. return null;
  469. }
  470. if (1 > $split_length = (int) $split_length) {
  471. if (80000 > \PHP_VERSION_ID) {
  472. trigger_error('The length of each segment must be greater than zero', \E_USER_WARNING);
  473. return false;
  474. }
  475. throw new \ValueError('Argument #2 ($length) must be greater than 0');
  476. }
  477. if (null === $encoding) {
  478. $encoding = mb_internal_encoding();
  479. }
  480. if ('UTF-8' === $encoding = self::getEncoding($encoding)) {
  481. $rx = '/(';
  482. while (65535 < $split_length) {
  483. $rx .= '.{65535}';
  484. $split_length -= 65535;
  485. }
  486. $rx .= '.{'.$split_length.'})/us';
  487. return preg_split($rx, $string, -1, \PREG_SPLIT_DELIM_CAPTURE | \PREG_SPLIT_NO_EMPTY);
  488. }
  489. $result = [];
  490. $length = mb_strlen($string, $encoding);
  491. for ($i = 0; $i < $length; $i += $split_length) {
  492. $result[] = mb_substr($string, $i, $split_length, $encoding);
  493. }
  494. return $result;
  495. }
  496. public static function mb_strtolower($s, $encoding = null)
  497. {
  498. return self::mb_convert_case($s, \MB_CASE_LOWER, $encoding);
  499. }
  500. public static function mb_strtoupper($s, $encoding = null)
  501. {
  502. return self::mb_convert_case($s, \MB_CASE_UPPER, $encoding);
  503. }
  504. public static function mb_substitute_character($c = null)
  505. {
  506. if (null === $c) {
  507. return 'none';
  508. }
  509. if (0 === strcasecmp($c, 'none')) {
  510. return true;
  511. }
  512. if (80000 > \PHP_VERSION_ID) {
  513. return false;
  514. }
  515. if (\is_int($c) || 'long' === $c || 'entity' === $c) {
  516. return false;
  517. }
  518. throw new \ValueError('Argument #1 ($substitute_character) must be "none", "long", "entity" or a valid codepoint');
  519. }
  520. public static function mb_substr($s, $start, $length = null, $encoding = null)
  521. {
  522. $encoding = self::getEncoding($encoding);
  523. if ('CP850' === $encoding || 'ASCII' === $encoding) {
  524. return (string) substr($s, $start, null === $length ? 2147483647 : $length);
  525. }
  526. if ($start < 0) {
  527. $start = iconv_strlen($s, $encoding) + $start;
  528. if ($start < 0) {
  529. $start = 0;
  530. }
  531. }
  532. if (null === $length) {
  533. $length = 2147483647;
  534. } elseif ($length < 0) {
  535. $length = iconv_strlen($s, $encoding) + $length - $start;
  536. if ($length < 0) {
  537. return '';
  538. }
  539. }
  540. return (string) iconv_substr($s, $start, $length, $encoding);
  541. }
  542. public static function mb_stripos($haystack, $needle, $offset = 0, $encoding = null)
  543. {
  544. [$haystack, $needle] = str_replace(self::SIMPLE_CASE_FOLD[0], self::SIMPLE_CASE_FOLD[1], [
  545. self::mb_convert_case($haystack, \MB_CASE_LOWER, $encoding),
  546. self::mb_convert_case($needle, \MB_CASE_LOWER, $encoding),
  547. ]);
  548. return self::mb_strpos($haystack, $needle, $offset, $encoding);
  549. }
  550. public static function mb_stristr($haystack, $needle, $part = false, $encoding = null)
  551. {
  552. $pos = self::mb_stripos($haystack, $needle, 0, $encoding);
  553. return self::getSubpart($pos, $part, $haystack, $encoding);
  554. }
  555. public static function mb_strrchr($haystack, $needle, $part = false, $encoding = null)
  556. {
  557. $encoding = self::getEncoding($encoding);
  558. if ('CP850' === $encoding || 'ASCII' === $encoding) {
  559. $pos = strrpos($haystack, $needle);
  560. } else {
  561. $needle = self::mb_substr($needle, 0, 1, $encoding);
  562. $pos = iconv_strrpos($haystack, $needle, $encoding);
  563. }
  564. return self::getSubpart($pos, $part, $haystack, $encoding);
  565. }
  566. public static function mb_strrichr($haystack, $needle, $part = false, $encoding = null)
  567. {
  568. $needle = self::mb_substr($needle, 0, 1, $encoding);
  569. $pos = self::mb_strripos($haystack, $needle, $encoding);
  570. return self::getSubpart($pos, $part, $haystack, $encoding);
  571. }
  572. public static function mb_strripos($haystack, $needle, $offset = 0, $encoding = null)
  573. {
  574. $haystack = self::mb_convert_case($haystack, \MB_CASE_LOWER, $encoding);
  575. $needle = self::mb_convert_case($needle, \MB_CASE_LOWER, $encoding);
  576. $haystack = str_replace(self::SIMPLE_CASE_FOLD[0], self::SIMPLE_CASE_FOLD[1], $haystack);
  577. $needle = str_replace(self::SIMPLE_CASE_FOLD[0], self::SIMPLE_CASE_FOLD[1], $needle);
  578. return self::mb_strrpos($haystack, $needle, $offset, $encoding);
  579. }
  580. public static function mb_strstr($haystack, $needle, $part = false, $encoding = null)
  581. {
  582. $pos = strpos($haystack, $needle);
  583. if (false === $pos) {
  584. return false;
  585. }
  586. if ($part) {
  587. return substr($haystack, 0, $pos);
  588. }
  589. return substr($haystack, $pos);
  590. }
  591. public static function mb_get_info($type = 'all')
  592. {
  593. $info = [
  594. 'internal_encoding' => self::$internalEncoding,
  595. 'http_output' => 'pass',
  596. 'http_output_conv_mimetypes' => '^(text/|application/xhtml\+xml)',
  597. 'func_overload' => 0,
  598. 'func_overload_list' => 'no overload',
  599. 'mail_charset' => 'UTF-8',
  600. 'mail_header_encoding' => 'BASE64',
  601. 'mail_body_encoding' => 'BASE64',
  602. 'illegal_chars' => 0,
  603. 'encoding_translation' => 'Off',
  604. 'language' => self::$language,
  605. 'detect_order' => self::$encodingList,
  606. 'substitute_character' => 'none',
  607. 'strict_detection' => 'Off',
  608. ];
  609. if ('all' === $type) {
  610. return $info;
  611. }
  612. if (isset($info[$type])) {
  613. return $info[$type];
  614. }
  615. return false;
  616. }
  617. public static function mb_http_input($type = '')
  618. {
  619. return false;
  620. }
  621. public static function mb_http_output($encoding = null)
  622. {
  623. return null !== $encoding ? 'pass' === $encoding : 'pass';
  624. }
  625. public static function mb_strwidth($s, $encoding = null)
  626. {
  627. $encoding = self::getEncoding($encoding);
  628. if ('UTF-8' !== $encoding) {
  629. $s = iconv($encoding, 'UTF-8//IGNORE', $s);
  630. }
  631. $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);
  632. return ($wide << 1) + iconv_strlen($s, 'UTF-8');
  633. }
  634. public static function mb_substr_count($haystack, $needle, $encoding = null)
  635. {
  636. return substr_count($haystack, $needle);
  637. }
  638. public static function mb_output_handler($contents, $status)
  639. {
  640. return $contents;
  641. }
  642. public static function mb_chr($code, $encoding = null)
  643. {
  644. if (0x80 > $code %= 0x200000) {
  645. $s = \chr($code);
  646. } elseif (0x800 > $code) {
  647. $s = \chr(0xC0 | $code >> 6).\chr(0x80 | $code & 0x3F);
  648. } elseif (0x10000 > $code) {
  649. $s = \chr(0xE0 | $code >> 12).\chr(0x80 | $code >> 6 & 0x3F).\chr(0x80 | $code & 0x3F);
  650. } else {
  651. $s = \chr(0xF0 | $code >> 18).\chr(0x80 | $code >> 12 & 0x3F).\chr(0x80 | $code >> 6 & 0x3F).\chr(0x80 | $code & 0x3F);
  652. }
  653. if ('UTF-8' !== $encoding = self::getEncoding($encoding)) {
  654. $s = mb_convert_encoding($s, $encoding, 'UTF-8');
  655. }
  656. return $s;
  657. }
  658. public static function mb_ord($s, $encoding = null)
  659. {
  660. if ('UTF-8' !== $encoding = self::getEncoding($encoding)) {
  661. $s = mb_convert_encoding($s, 'UTF-8', $encoding);
  662. }
  663. if (1 === \strlen($s)) {
  664. return \ord($s);
  665. }
  666. $code = ($s = unpack('C*', substr($s, 0, 4))) ? $s[1] : 0;
  667. if (0xF0 <= $code) {
  668. return (($code - 0xF0) << 18) + (($s[2] - 0x80) << 12) + (($s[3] - 0x80) << 6) + $s[4] - 0x80;
  669. }
  670. if (0xE0 <= $code) {
  671. return (($code - 0xE0) << 12) + (($s[2] - 0x80) << 6) + $s[3] - 0x80;
  672. }
  673. if (0xC0 <= $code) {
  674. return (($code - 0xC0) << 6) + $s[2] - 0x80;
  675. }
  676. return $code;
  677. }
  678. public static function mb_str_pad(string $string, int $length, string $pad_string = ' ', int $pad_type = \STR_PAD_RIGHT, string $encoding = null): string
  679. {
  680. if (!\in_array($pad_type, [\STR_PAD_RIGHT, \STR_PAD_LEFT, \STR_PAD_BOTH], true)) {
  681. throw new \ValueError('mb_str_pad(): Argument #4 ($pad_type) must be STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH');
  682. }
  683. if (null === $encoding) {
  684. $encoding = self::mb_internal_encoding();
  685. }
  686. try {
  687. $validEncoding = @self::mb_check_encoding('', $encoding);
  688. } catch (\ValueError $e) {
  689. throw new \ValueError(sprintf('mb_str_pad(): Argument #5 ($encoding) must be a valid encoding, "%s" given', $encoding));
  690. }
  691. // BC for PHP 7.3 and lower
  692. if (!$validEncoding) {
  693. throw new \ValueError(sprintf('mb_str_pad(): Argument #5 ($encoding) must be a valid encoding, "%s" given', $encoding));
  694. }
  695. if (self::mb_strlen($pad_string, $encoding) <= 0) {
  696. throw new \ValueError('mb_str_pad(): Argument #3 ($pad_string) must be a non-empty string');
  697. }
  698. $paddingRequired = $length - self::mb_strlen($string, $encoding);
  699. if ($paddingRequired < 1) {
  700. return $string;
  701. }
  702. switch ($pad_type) {
  703. case \STR_PAD_LEFT:
  704. return self::mb_substr(str_repeat($pad_string, $paddingRequired), 0, $paddingRequired, $encoding).$string;
  705. case \STR_PAD_RIGHT:
  706. return $string.self::mb_substr(str_repeat($pad_string, $paddingRequired), 0, $paddingRequired, $encoding);
  707. default:
  708. $leftPaddingLength = floor($paddingRequired / 2);
  709. $rightPaddingLength = $paddingRequired - $leftPaddingLength;
  710. return self::mb_substr(str_repeat($pad_string, $leftPaddingLength), 0, $leftPaddingLength, $encoding).$string.self::mb_substr(str_repeat($pad_string, $rightPaddingLength), 0, $rightPaddingLength, $encoding);
  711. }
  712. }
  713. private static function getSubpart($pos, $part, $haystack, $encoding)
  714. {
  715. if (false === $pos) {
  716. return false;
  717. }
  718. if ($part) {
  719. return self::mb_substr($haystack, 0, $pos, $encoding);
  720. }
  721. return self::mb_substr($haystack, $pos, null, $encoding);
  722. }
  723. private static function html_encoding_callback(array $m)
  724. {
  725. $i = 1;
  726. $entities = '';
  727. $m = unpack('C*', htmlentities($m[0], \ENT_COMPAT, 'UTF-8'));
  728. while (isset($m[$i])) {
  729. if (0x80 > $m[$i]) {
  730. $entities .= \chr($m[$i++]);
  731. continue;
  732. }
  733. if (0xF0 <= $m[$i]) {
  734. $c = (($m[$i++] - 0xF0) << 18) + (($m[$i++] - 0x80) << 12) + (($m[$i++] - 0x80) << 6) + $m[$i++] - 0x80;
  735. } elseif (0xE0 <= $m[$i]) {
  736. $c = (($m[$i++] - 0xE0) << 12) + (($m[$i++] - 0x80) << 6) + $m[$i++] - 0x80;
  737. } else {
  738. $c = (($m[$i++] - 0xC0) << 6) + $m[$i++] - 0x80;
  739. }
  740. $entities .= '&#'.$c.';';
  741. }
  742. return $entities;
  743. }
  744. private static function title_case(array $s)
  745. {
  746. return self::mb_convert_case($s[1], \MB_CASE_UPPER, 'UTF-8').self::mb_convert_case($s[2], \MB_CASE_LOWER, 'UTF-8');
  747. }
  748. private static function getData($file)
  749. {
  750. if (file_exists($file = __DIR__.'/Resources/unidata/'.$file.'.php')) {
  751. return require $file;
  752. }
  753. return false;
  754. }
  755. private static function getEncoding($encoding)
  756. {
  757. if (null === $encoding) {
  758. return self::$internalEncoding;
  759. }
  760. if ('UTF-8' === $encoding) {
  761. return 'UTF-8';
  762. }
  763. $encoding = strtoupper($encoding);
  764. if ('8BIT' === $encoding || 'BINARY' === $encoding) {
  765. return 'CP850';
  766. }
  767. if ('UTF8' === $encoding) {
  768. return 'UTF-8';
  769. }
  770. return $encoding;
  771. }
  772. }