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