4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Polyfill\Mbstring;
15 * Partial mbstring implementation in PHP, iconv based, UTF-8 centric.
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_strtolower - Make a string lowercase
39 * - mb_strtoupper - Make a string uppercase
40 * - mb_substitute_character - Set/Get substitution character
41 * - mb_substr - Get part of string
42 * - mb_stripos - Finds position of first occurrence of a string within another, case insensitive
43 * - mb_stristr - Finds first occurrence of a string within another, case insensitive
44 * - mb_strrchr - Finds the last occurrence of a character in a string within another
45 * - mb_strrichr - Finds the last occurrence of a character in a string within another, case insensitive
46 * - mb_strripos - Finds position of last occurrence of a string within another, case insensitive
47 * - mb_strstr - Finds first occurrence of a string within another
48 * - mb_strwidth - Return width of string
49 * - mb_substr_count - Count the number of substring occurrences
52 * - mb_convert_kana - Convert "kana" one from another ("zen-kaku", "han-kaku" and more)
53 * - mb_ereg_* - Regular expression with multibyte support
54 * - mb_parse_str - Parse GET/POST/COOKIE data and set global variable
55 * - mb_preferred_mime_name - Get MIME charset string
56 * - mb_regex_encoding - Returns current encoding for multibyte regex as string
57 * - mb_regex_set_options - Set/Get the default options for mbregex functions
58 * - mb_send_mail - Send encoded mail
59 * - mb_split - Split multibyte string using regular expression
60 * - mb_strcut - Get part of string
61 * - mb_strimwidth - Get truncated string with specified width
63 * @author Nicolas Grekas <p@tchwork.com>
69 const MB_CASE_FOLD = PHP_INT_MAX;
71 private static $encodingList = array('ASCII', 'UTF-8');
72 private static $language = 'neutral';
73 private static $internalEncoding = 'UTF-8';
74 private static $caseFold = array(
75 array('µ', 'ſ', "\xCD\x85", 'ς', "\xCF\x90", "\xCF\x91", "\xCF\x95", "\xCF\x96", "\xCF\xB0", "\xCF\xB1", "\xCF\xB5", "\xE1\xBA\x9B", "\xE1\xBE\xBE"),
76 array('μ', 's', 'ι', 'σ', 'β', 'θ', 'φ', 'π', 'κ', 'ρ', 'ε', "\xE1\xB9\xA1", 'ι'),
79 public static function mb_convert_encoding($s, $toEncoding, $fromEncoding = null)
81 if (\is_array($fromEncoding) || false !== strpos($fromEncoding, ',')) {
82 $fromEncoding = self::mb_detect_encoding($s, $fromEncoding);
84 $fromEncoding = self::getEncoding($fromEncoding);
87 $toEncoding = self::getEncoding($toEncoding);
89 if ('BASE64' === $fromEncoding) {
90 $s = base64_decode($s);
91 $fromEncoding = $toEncoding;
94 if ('BASE64' === $toEncoding) {
95 return base64_encode($s);
98 if ('HTML-ENTITIES' === $toEncoding || 'HTML' === $toEncoding) {
99 if ('HTML-ENTITIES' === $fromEncoding || 'HTML' === $fromEncoding) {
100 $fromEncoding = 'Windows-1252';
102 if ('UTF-8' !== $fromEncoding) {
103 $s = iconv($fromEncoding, 'UTF-8//IGNORE', $s);
106 return preg_replace_callback('/[\x80-\xFF]+/', array(__CLASS__, 'html_encoding_callback'), $s);
109 if ('HTML-ENTITIES' === $fromEncoding) {
110 $s = html_entity_decode($s, ENT_COMPAT, 'UTF-8');
111 $fromEncoding = 'UTF-8';
114 return iconv($fromEncoding, $toEncoding.'//IGNORE', $s);
117 public static function mb_convert_variables($toEncoding, $fromEncoding, &$a = null, &$b = null, &$c = null, &$d = null, &$e = null, &$f = null)
119 $vars = array(&$a, &$b, &$c, &$d, &$e, &$f);
122 array_walk_recursive($vars, function (&$v) use (&$ok, $toEncoding, $fromEncoding) {
123 if (false === $v = Mbstring::mb_convert_encoding($v, $toEncoding, $fromEncoding)) {
128 return $ok ? $fromEncoding : false;
131 public static function mb_decode_mimeheader($s)
133 return iconv_mime_decode($s, 2, self::$internalEncoding);
136 public static function mb_encode_mimeheader($s, $charset = null, $transferEncoding = null, $linefeed = null, $indent = null)
138 trigger_error('mb_encode_mimeheader() is bugged. Please use iconv_mime_encode() instead', E_USER_WARNING);
141 public static function mb_decode_numericentity($s, $convmap, $encoding = null)
143 if (null !== $s && !\is_scalar($s) && !(\is_object($s) && \method_exists($s, '__toString'))) {
144 trigger_error('mb_decode_numericentity() expects parameter 1 to be string, '.\gettype($s).' given', E_USER_WARNING);
149 if (!\is_array($convmap) || !$convmap) {
153 if (null !== $encoding && !\is_scalar($encoding)) {
154 trigger_error('mb_decode_numericentity() expects parameter 3 to be string, '.\gettype($s).' given', E_USER_WARNING);
156 return ''; // Instead of null (cf. mb_encode_numericentity).
164 $encoding = self::getEncoding($encoding);
166 if ('UTF-8' === $encoding) {
168 if (!preg_match('//u', $s)) {
169 $s = @iconv('UTF-8', 'UTF-8//IGNORE', $s);
172 $s = iconv($encoding, 'UTF-8//IGNORE', $s);
175 $cnt = floor(\count($convmap) / 4) * 4;
177 for ($i = 0; $i < $cnt; $i += 4) {
178 // collector_decode_htmlnumericentity ignores $convmap[$i + 3]
179 $convmap[$i] += $convmap[$i + 2];
180 $convmap[$i + 1] += $convmap[$i + 2];
183 $s = preg_replace_callback('/&#(?:0*([0-9]+)|x0*([0-9a-fA-F]+))(?!&);?/', function (array $m) use ($cnt, $convmap) {
184 $c = isset($m[2]) ? (int) hexdec($m[2]) : $m[1];
185 for ($i = 0; $i < $cnt; $i += 4) {
186 if ($c >= $convmap[$i] && $c <= $convmap[$i + 1]) {
187 return Mbstring::mb_chr($c - $convmap[$i + 2]);
194 if (null === $encoding) {
198 return iconv('UTF-8', $encoding.'//IGNORE', $s);
201 public static function mb_encode_numericentity($s, $convmap, $encoding = null, $is_hex = false)
203 if (null !== $s && !\is_scalar($s) && !(\is_object($s) && \method_exists($s, '__toString'))) {
204 trigger_error('mb_encode_numericentity() expects parameter 1 to be string, '.\gettype($s).' given', E_USER_WARNING);
209 if (!\is_array($convmap) || !$convmap) {
213 if (null !== $encoding && !\is_scalar($encoding)) {
214 trigger_error('mb_encode_numericentity() expects parameter 3 to be string, '.\gettype($s).' given', E_USER_WARNING);
216 return null; // Instead of '' (cf. mb_decode_numericentity).
219 if (null !== $is_hex && !\is_scalar($is_hex)) {
220 trigger_error('mb_encode_numericentity() expects parameter 4 to be boolean, '.\gettype($s).' given', E_USER_WARNING);
230 $encoding = self::getEncoding($encoding);
232 if ('UTF-8' === $encoding) {
234 if (!preg_match('//u', $s)) {
235 $s = @iconv('UTF-8', 'UTF-8//IGNORE', $s);
238 $s = iconv($encoding, 'UTF-8//IGNORE', $s);
241 static $ulenMask = array("\xC0" => 2, "\xD0" => 2, "\xE0" => 3, "\xF0" => 4);
243 $cnt = floor(\count($convmap) / 4) * 4;
249 $ulen = $s[$i] < "\x80" ? 1 : $ulenMask[$s[$i] & "\xF0"];
250 $uchr = substr($s, $i, $ulen);
252 $c = self::mb_ord($uchr);
254 for ($j = 0; $j < $cnt; $j += 4) {
255 if ($c >= $convmap[$j] && $c <= $convmap[$j + 1]) {
256 $cOffset = ($c + $convmap[$j + 2]) & $convmap[$j + 3];
257 $result .= $is_hex ? sprintf('&#x%X;', $cOffset) : '&#'.$cOffset.';';
264 if (null === $encoding) {
268 return iconv('UTF-8', $encoding.'//IGNORE', $result);
271 public static function mb_convert_case($s, $mode, $encoding = null)
278 $encoding = self::getEncoding($encoding);
280 if ('UTF-8' === $encoding) {
282 if (!preg_match('//u', $s)) {
283 $s = @iconv('UTF-8', 'UTF-8//IGNORE', $s);
286 $s = iconv($encoding, 'UTF-8//IGNORE', $s);
289 if (MB_CASE_TITLE == $mode) {
290 static $titleRegexp = null;
291 if (null === $titleRegexp) {
292 $titleRegexp = self::getData('titleCaseRegexp');
294 $s = preg_replace_callback($titleRegexp, array(__CLASS__, 'title_case'), $s);
296 if (MB_CASE_UPPER == $mode) {
297 static $upper = null;
298 if (null === $upper) {
299 $upper = self::getData('upperCase');
303 if (self::MB_CASE_FOLD === $mode) {
304 $s = str_replace(self::$caseFold[0], self::$caseFold[1], $s);
307 static $lower = null;
308 if (null === $lower) {
309 $lower = self::getData('lowerCase');
314 static $ulenMask = array("\xC0" => 2, "\xD0" => 2, "\xE0" => 3, "\xF0" => 4);
320 $ulen = $s[$i] < "\x80" ? 1 : $ulenMask[$s[$i] & "\xF0"];
321 $uchr = substr($s, $i, $ulen);
324 if (isset($map[$uchr])) {
326 $nlen = \strlen($uchr);
328 if ($nlen == $ulen) {
331 $s[--$nlen] = $uchr[--$ulen];
334 $s = substr_replace($s, $uchr, $i - $ulen, $ulen);
335 $len += $nlen - $ulen;
342 if (null === $encoding) {
346 return iconv('UTF-8', $encoding.'//IGNORE', $s);
349 public static function mb_internal_encoding($encoding = null)
351 if (null === $encoding) {
352 return self::$internalEncoding;
355 $encoding = self::getEncoding($encoding);
357 if ('UTF-8' === $encoding || false !== @iconv($encoding, $encoding, ' ')) {
358 self::$internalEncoding = $encoding;
366 public static function mb_language($lang = null)
368 if (null === $lang) {
369 return self::$language;
372 switch ($lang = strtolower($lang)) {
375 self::$language = $lang;
383 public static function mb_list_encodings()
385 return array('UTF-8');
388 public static function mb_encoding_aliases($encoding)
390 switch (strtoupper($encoding)) {
393 return array('utf8');
399 public static function mb_check_encoding($var = null, $encoding = null)
401 if (null === $encoding) {
405 $encoding = self::$internalEncoding;
408 return self::mb_detect_encoding($var, array($encoding)) || false !== @iconv($encoding, $encoding, $var);
411 public static function mb_detect_encoding($str, $encodingList = null, $strict = false)
413 if (null === $encodingList) {
414 $encodingList = self::$encodingList;
416 if (!\is_array($encodingList)) {
417 $encodingList = array_map('trim', explode(',', $encodingList));
419 $encodingList = array_map('strtoupper', $encodingList);
422 foreach ($encodingList as $enc) {
425 if (!preg_match('/[\x80-\xFF]/', $str)) {
432 if (preg_match('//u', $str)) {
438 if (0 === strncmp($enc, 'ISO-8859-', 9)) {
447 public static function mb_detect_order($encodingList = null)
449 if (null === $encodingList) {
450 return self::$encodingList;
453 if (!\is_array($encodingList)) {
454 $encodingList = array_map('trim', explode(',', $encodingList));
456 $encodingList = array_map('strtoupper', $encodingList);
458 foreach ($encodingList as $enc) {
461 if (strncmp($enc, 'ISO-8859-', 9)) {
471 self::$encodingList = $encodingList;
476 public static function mb_strlen($s, $encoding = null)
478 $encoding = self::getEncoding($encoding);
479 if ('CP850' === $encoding || 'ASCII' === $encoding) {
483 return @iconv_strlen($s, $encoding);
486 public static function mb_strpos($haystack, $needle, $offset = 0, $encoding = null)
488 $encoding = self::getEncoding($encoding);
489 if ('CP850' === $encoding || 'ASCII' === $encoding) {
490 return strpos($haystack, $needle, $offset);
493 $needle = (string) $needle;
494 if ('' === $needle) {
495 trigger_error(__METHOD__.': Empty delimiter', E_USER_WARNING);
500 return iconv_strpos($haystack, $needle, $offset, $encoding);
503 public static function mb_strrpos($haystack, $needle, $offset = 0, $encoding = null)
505 $encoding = self::getEncoding($encoding);
506 if ('CP850' === $encoding || 'ASCII' === $encoding) {
507 return strrpos($haystack, $needle, $offset);
510 if ($offset != (int) $offset) {
512 } elseif ($offset = (int) $offset) {
514 $haystack = self::mb_substr($haystack, 0, $offset, $encoding);
517 $haystack = self::mb_substr($haystack, $offset, 2147483647, $encoding);
521 $pos = iconv_strrpos($haystack, $needle, $encoding);
523 return false !== $pos ? $offset + $pos : false;
526 public static function mb_strtolower($s, $encoding = null)
528 return self::mb_convert_case($s, MB_CASE_LOWER, $encoding);
531 public static function mb_strtoupper($s, $encoding = null)
533 return self::mb_convert_case($s, MB_CASE_UPPER, $encoding);
536 public static function mb_substitute_character($c = null)
538 if (0 === strcasecmp($c, 'none')) {
542 return null !== $c ? false : 'none';
545 public static function mb_substr($s, $start, $length = null, $encoding = null)
547 $encoding = self::getEncoding($encoding);
548 if ('CP850' === $encoding || 'ASCII' === $encoding) {
549 return substr($s, $start, null === $length ? 2147483647 : $length);
553 $start = iconv_strlen($s, $encoding) + $start;
559 if (null === $length) {
560 $length = 2147483647;
561 } elseif ($length < 0) {
562 $length = iconv_strlen($s, $encoding) + $length - $start;
568 return (string) iconv_substr($s, $start, $length, $encoding);
571 public static function mb_stripos($haystack, $needle, $offset = 0, $encoding = null)
573 $haystack = self::mb_convert_case($haystack, self::MB_CASE_FOLD, $encoding);
574 $needle = self::mb_convert_case($needle, self::MB_CASE_FOLD, $encoding);
576 return self::mb_strpos($haystack, $needle, $offset, $encoding);
579 public static function mb_stristr($haystack, $needle, $part = false, $encoding = null)
581 $pos = self::mb_stripos($haystack, $needle, 0, $encoding);
583 return self::getSubpart($pos, $part, $haystack, $encoding);
586 public static function mb_strrchr($haystack, $needle, $part = false, $encoding = null)
588 $encoding = self::getEncoding($encoding);
589 if ('CP850' === $encoding || 'ASCII' === $encoding) {
590 return strrchr($haystack, $needle, $part);
592 $needle = self::mb_substr($needle, 0, 1, $encoding);
593 $pos = iconv_strrpos($haystack, $needle, $encoding);
595 return self::getSubpart($pos, $part, $haystack, $encoding);
598 public static function mb_strrichr($haystack, $needle, $part = false, $encoding = null)
600 $needle = self::mb_substr($needle, 0, 1, $encoding);
601 $pos = self::mb_strripos($haystack, $needle, $encoding);
603 return self::getSubpart($pos, $part, $haystack, $encoding);
606 public static function mb_strripos($haystack, $needle, $offset = 0, $encoding = null)
608 $haystack = self::mb_convert_case($haystack, self::MB_CASE_FOLD, $encoding);
609 $needle = self::mb_convert_case($needle, self::MB_CASE_FOLD, $encoding);
611 return self::mb_strrpos($haystack, $needle, $offset, $encoding);
614 public static function mb_strstr($haystack, $needle, $part = false, $encoding = null)
616 $pos = strpos($haystack, $needle);
617 if (false === $pos) {
621 return substr($haystack, 0, $pos);
624 return substr($haystack, $pos);
627 public static function mb_get_info($type = 'all')
630 'internal_encoding' => self::$internalEncoding,
631 'http_output' => 'pass',
632 'http_output_conv_mimetypes' => '^(text/|application/xhtml\+xml)',
633 'func_overload' => 0,
634 'func_overload_list' => 'no overload',
635 'mail_charset' => 'UTF-8',
636 'mail_header_encoding' => 'BASE64',
637 'mail_body_encoding' => 'BASE64',
638 'illegal_chars' => 0,
639 'encoding_translation' => 'Off',
640 'language' => self::$language,
641 'detect_order' => self::$encodingList,
642 'substitute_character' => 'none',
643 'strict_detection' => 'Off',
646 if ('all' === $type) {
649 if (isset($info[$type])) {
656 public static function mb_http_input($type = '')
661 public static function mb_http_output($encoding = null)
663 return null !== $encoding ? 'pass' === $encoding : 'pass';
666 public static function mb_strwidth($s, $encoding = null)
668 $encoding = self::getEncoding($encoding);
670 if ('UTF-8' !== $encoding) {
671 $s = iconv($encoding, 'UTF-8//IGNORE', $s);
674 $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);
676 return ($wide << 1) + iconv_strlen($s, 'UTF-8');
679 public static function mb_substr_count($haystack, $needle, $encoding = null)
681 return substr_count($haystack, $needle);
684 public static function mb_output_handler($contents, $status)
689 public static function mb_chr($code, $encoding = null)
691 if (0x80 > $code %= 0x200000) {
693 } elseif (0x800 > $code) {
694 $s = \chr(0xC0 | $code >> 6).\chr(0x80 | $code & 0x3F);
695 } elseif (0x10000 > $code) {
696 $s = \chr(0xE0 | $code >> 12).\chr(0x80 | $code >> 6 & 0x3F).\chr(0x80 | $code & 0x3F);
698 $s = \chr(0xF0 | $code >> 18).\chr(0x80 | $code >> 12 & 0x3F).\chr(0x80 | $code >> 6 & 0x3F).\chr(0x80 | $code & 0x3F);
701 if ('UTF-8' !== $encoding = self::getEncoding($encoding)) {
702 $s = mb_convert_encoding($s, $encoding, 'UTF-8');
708 public static function mb_ord($s, $encoding = null)
710 if ('UTF-8' !== $encoding = self::getEncoding($encoding)) {
711 $s = mb_convert_encoding($s, 'UTF-8', $encoding);
714 if (1 === \strlen($s)) {
718 $code = ($s = unpack('C*', substr($s, 0, 4))) ? $s[1] : 0;
720 return (($code - 0xF0) << 18) + (($s[2] - 0x80) << 12) + (($s[3] - 0x80) << 6) + $s[4] - 0x80;
723 return (($code - 0xE0) << 12) + (($s[2] - 0x80) << 6) + $s[3] - 0x80;
726 return (($code - 0xC0) << 6) + $s[2] - 0x80;
732 private static function getSubpart($pos, $part, $haystack, $encoding)
734 if (false === $pos) {
738 return self::mb_substr($haystack, 0, $pos, $encoding);
741 return self::mb_substr($haystack, $pos, null, $encoding);
744 private static function html_encoding_callback(array $m)
748 $m = unpack('C*', htmlentities($m[0], ENT_COMPAT, 'UTF-8'));
750 while (isset($m[$i])) {
752 $entities .= \chr($m[$i++]);
755 if (0xF0 <= $m[$i]) {
756 $c = (($m[$i++] - 0xF0) << 18) + (($m[$i++] - 0x80) << 12) + (($m[$i++] - 0x80) << 6) + $m[$i++] - 0x80;
757 } elseif (0xE0 <= $m[$i]) {
758 $c = (($m[$i++] - 0xE0) << 12) + (($m[$i++] - 0x80) << 6) + $m[$i++] - 0x80;
760 $c = (($m[$i++] - 0xC0) << 6) + $m[$i++] - 0x80;
763 $entities .= '&#'.$c.';';
769 private static function title_case(array $s)
771 return self::mb_convert_case($s[1], MB_CASE_UPPER, 'UTF-8').self::mb_convert_case($s[2], MB_CASE_LOWER, 'UTF-8');
774 private static function getData($file)
776 if (file_exists($file = __DIR__.'/Resources/unidata/'.$file.'.php')) {
777 return require $file;
783 private static function getEncoding($encoding)
785 if (null === $encoding) {
786 return self::$internalEncoding;
789 $encoding = strtoupper($encoding);
791 if ('8BIT' === $encoding || 'BINARY' === $encoding) {
794 if ('UTF8' === $encoding) {