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_convert_case - Perform case folding on a string
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_strtolower - Make a string lowercase
36 * - mb_strtoupper - Make a string uppercase
37 * - mb_substitute_character - Set/Get substitution character
38 * - mb_substr - Get part of string
39 * - mb_stripos - Finds position of first occurrence of a string within another, case insensitive
40 * - mb_stristr - Finds first occurrence of a string within another, case insensitive
41 * - mb_strrchr - Finds the last occurrence of a character in a string within another
42 * - mb_strrichr - Finds the last occurrence of a character in a string within another, case insensitive
43 * - mb_strripos - Finds position of last occurrence of a string within another, case insensitive
44 * - mb_strstr - Finds first occurrence of a string within anothers
45 * - mb_strwidth - Return width of string
46 * - mb_substr_count - Count the number of substring occurrences
49 * - mb_convert_kana - Convert "kana" one from another ("zen-kaku", "han-kaku" and more)
50 * - mb_decode_numericentity - Decode HTML numeric string reference to character
51 * - mb_encode_numericentity - Encode character to HTML numeric string reference
52 * - mb_ereg_* - Regular expression with multibyte support
53 * - mb_parse_str - Parse GET/POST/COOKIE data and set global variable
54 * - mb_preferred_mime_name - Get MIME charset string
55 * - mb_regex_encoding - Returns current encoding for multibyte regex as string
56 * - mb_regex_set_options - Set/Get the default options for mbregex functions
57 * - mb_send_mail - Send encoded mail
58 * - mb_split - Split multibyte string using regular expression
59 * - mb_strcut - Get part of string
60 * - mb_strimwidth - Get truncated string with specified width
62 * @author Nicolas Grekas <p@tchwork.com>
68 const MB_CASE_FOLD = PHP_INT_MAX;
70 private static $encodingList = array('ASCII', 'UTF-8');
71 private static $language = 'neutral';
72 private static $internalEncoding = 'UTF-8';
73 private static $caseFold = array(
74 array('µ','ſ',"\xCD\x85",'ς',"\xCF\x90","\xCF\x91","\xCF\x95","\xCF\x96","\xCF\xB0","\xCF\xB1","\xCF\xB5","\xE1\xBA\x9B","\xE1\xBE\xBE"),
75 array('μ','s','ι', 'σ','β', 'θ', 'φ', 'π', 'κ', 'ρ', 'ε', "\xE1\xB9\xA1",'ι'),
78 public static function mb_convert_encoding($s, $toEncoding, $fromEncoding = null)
80 if (is_array($fromEncoding) || false !== strpos($fromEncoding, ',')) {
81 $fromEncoding = self::mb_detect_encoding($s, $fromEncoding);
83 $fromEncoding = self::getEncoding($fromEncoding);
86 $toEncoding = self::getEncoding($toEncoding);
88 if ('BASE64' === $fromEncoding) {
89 $s = base64_decode($s);
90 $fromEncoding = $toEncoding;
93 if ('BASE64' === $toEncoding) {
94 return base64_encode($s);
97 if ('HTML-ENTITIES' === $toEncoding || 'HTML' === $toEncoding) {
98 if ('HTML-ENTITIES' === $fromEncoding || 'HTML' === $fromEncoding) {
99 $fromEncoding = 'Windows-1252';
101 if ('UTF-8' !== $fromEncoding) {
102 $s = iconv($fromEncoding, 'UTF-8//IGNORE', $s);
105 return preg_replace_callback('/[\x80-\xFF]+/', array(__CLASS__, 'html_encoding_callback'), $s);
108 if ('HTML-ENTITIES' === $fromEncoding) {
109 $s = html_entity_decode($s, ENT_COMPAT, 'UTF-8');
110 $fromEncoding = 'UTF-8';
113 return iconv($fromEncoding, $toEncoding.'//IGNORE', $s);
116 public static function mb_convert_variables($toEncoding, $fromEncoding, &$a = null, &$b = null, &$c = null, &$d = null, &$e = null, &$f = null)
118 $vars = array(&$a, &$b, &$c, &$d, &$e, &$f);
121 array_walk_recursive($vars, function (&$v) use (&$ok, $toEncoding, $fromEncoding) {
122 if (false === $v = Mbstring::mb_convert_encoding($v, $toEncoding, $fromEncoding)) {
127 return $ok ? $fromEncoding : false;
130 public static function mb_decode_mimeheader($s)
132 return iconv_mime_decode($s, 2, self::$internalEncoding);
135 public static function mb_encode_mimeheader($s, $charset = null, $transferEncoding = null, $linefeed = null, $indent = null)
137 trigger_error('mb_encode_mimeheader() is bugged. Please use iconv_mime_encode() instead', E_USER_WARNING);
140 public static function mb_convert_case($s, $mode, $encoding = null)
142 if ('' === $s .= '') {
146 $encoding = self::getEncoding($encoding);
148 if ('UTF-8' === $encoding) {
150 if (!preg_match('//u', $s)) {
151 $s = @iconv('UTF-8', 'UTF-8//IGNORE', $s);
154 $s = iconv($encoding, 'UTF-8//IGNORE', $s);
157 if (MB_CASE_TITLE == $mode) {
158 $s = preg_replace_callback('/\b\p{Ll}/u', array(__CLASS__, 'title_case_upper'), $s);
159 $s = preg_replace_callback('/\B[\p{Lu}\p{Lt}]+/u', array(__CLASS__, 'title_case_lower'), $s);
161 if (MB_CASE_UPPER == $mode) {
162 static $upper = null;
163 if (null === $upper) {
164 $upper = self::getData('upperCase');
168 if (self::MB_CASE_FOLD === $mode) {
169 $s = str_replace(self::$caseFold[0], self::$caseFold[1], $s);
172 static $lower = null;
173 if (null === $lower) {
174 $lower = self::getData('lowerCase');
179 static $ulenMask = array("\xC0" => 2, "\xD0" => 2, "\xE0" => 3, "\xF0" => 4);
185 $ulen = $s[$i] < "\x80" ? 1 : $ulenMask[$s[$i] & "\xF0"];
186 $uchr = substr($s, $i, $ulen);
189 if (isset($map[$uchr])) {
191 $nlen = strlen($uchr);
193 if ($nlen == $ulen) {
196 $s[--$nlen] = $uchr[--$ulen];
199 $s = substr_replace($s, $uchr, $i - $ulen, $ulen);
200 $len += $nlen - $ulen;
207 if (null === $encoding) {
211 return iconv('UTF-8', $encoding.'//IGNORE', $s);
214 public static function mb_internal_encoding($encoding = null)
216 if (null === $encoding) {
217 return self::$internalEncoding;
220 $encoding = self::getEncoding($encoding);
222 if ('UTF-8' === $encoding || false !== @iconv($encoding, $encoding, ' ')) {
223 self::$internalEncoding = $encoding;
231 public static function mb_language($lang = null)
233 if (null === $lang) {
234 return self::$language;
237 switch ($lang = strtolower($lang)) {
240 self::$language = $lang;
248 public static function mb_list_encodings()
250 return array('UTF-8');
253 public static function mb_encoding_aliases($encoding)
255 switch (strtoupper($encoding)) {
258 return array('utf8');
264 public static function mb_check_encoding($var = null, $encoding = null)
266 if (null === $encoding) {
270 $encoding = self::$internalEncoding;
273 return self::mb_detect_encoding($var, array($encoding)) || false !== @iconv($encoding, $encoding, $var);
276 public static function mb_detect_encoding($str, $encodingList = null, $strict = false)
278 if (null === $encodingList) {
279 $encodingList = self::$encodingList;
281 if (!is_array($encodingList)) {
282 $encodingList = array_map('trim', explode(',', $encodingList));
284 $encodingList = array_map('strtoupper', $encodingList);
287 foreach ($encodingList as $enc) {
290 if (!preg_match('/[\x80-\xFF]/', $str)) {
297 if (preg_match('//u', $str)) {
303 if (0 === strncmp($enc, 'ISO-8859-', 9)) {
312 public static function mb_detect_order($encodingList = null)
314 if (null === $encodingList) {
315 return self::$encodingList;
318 if (!is_array($encodingList)) {
319 $encodingList = array_map('trim', explode(',', $encodingList));
321 $encodingList = array_map('strtoupper', $encodingList);
323 foreach ($encodingList as $enc) {
326 if (strncmp($enc, 'ISO-8859-', 9)) {
335 self::$encodingList = $encodingList;
340 public static function mb_strlen($s, $encoding = null)
342 $encoding = self::getEncoding($encoding);
343 if ('CP850' === $encoding || 'ASCII' === $encoding) {
347 return @iconv_strlen($s, $encoding);
350 public static function mb_strpos($haystack, $needle, $offset = 0, $encoding = null)
352 $encoding = self::getEncoding($encoding);
353 if ('CP850' === $encoding || 'ASCII' === $encoding) {
354 return strpos($haystack, $needle, $offset);
357 if ('' === $needle .= '') {
358 trigger_error(__METHOD__.': Empty delimiter', E_USER_WARNING);
363 return iconv_strpos($haystack, $needle, $offset, $encoding);
366 public static function mb_strrpos($haystack, $needle, $offset = 0, $encoding = null)
368 $encoding = self::getEncoding($encoding);
369 if ('CP850' === $encoding || 'ASCII' === $encoding) {
370 return strrpos($haystack, $needle, $offset);
373 if ($offset != (int) $offset) {
375 } elseif ($offset = (int) $offset) {
377 $haystack = self::mb_substr($haystack, 0, $offset, $encoding);
380 $haystack = self::mb_substr($haystack, $offset, 2147483647, $encoding);
384 $pos = iconv_strrpos($haystack, $needle, $encoding);
386 return false !== $pos ? $offset + $pos : false;
389 public static function mb_strtolower($s, $encoding = null)
391 return self::mb_convert_case($s, MB_CASE_LOWER, $encoding);
394 public static function mb_strtoupper($s, $encoding = null)
396 return self::mb_convert_case($s, MB_CASE_UPPER, $encoding);
399 public static function mb_substitute_character($c = null)
401 if (0 === strcasecmp($c, 'none')) {
405 return null !== $c ? false : 'none';
408 public static function mb_substr($s, $start, $length = null, $encoding = null)
410 $encoding = self::getEncoding($encoding);
411 if ('CP850' === $encoding || 'ASCII' === $encoding) {
412 return substr($s, $start, null === $length ? 2147483647 : $length);
416 $start = iconv_strlen($s, $encoding) + $start;
422 if (null === $length) {
423 $length = 2147483647;
424 } elseif ($length < 0) {
425 $length = iconv_strlen($s, $encoding) + $length - $start;
431 return iconv_substr($s, $start, $length, $encoding).'';
434 public static function mb_stripos($haystack, $needle, $offset = 0, $encoding = null)
436 $haystack = self::mb_convert_case($haystack, self::MB_CASE_FOLD, $encoding);
437 $needle = self::mb_convert_case($needle, self::MB_CASE_FOLD, $encoding);
439 return self::mb_strpos($haystack, $needle, $offset, $encoding);
442 public static function mb_stristr($haystack, $needle, $part = false, $encoding = null)
444 $pos = self::mb_stripos($haystack, $needle, 0, $encoding);
446 return self::getSubpart($pos, $part, $haystack, $encoding);
449 public static function mb_strrchr($haystack, $needle, $part = false, $encoding = null)
451 $encoding = self::getEncoding($encoding);
452 if ('CP850' === $encoding || 'ASCII' === $encoding) {
453 return strrchr($haystack, $needle, $part);
455 $needle = self::mb_substr($needle, 0, 1, $encoding);
456 $pos = iconv_strrpos($haystack, $needle, $encoding);
458 return self::getSubpart($pos, $part, $haystack, $encoding);
461 public static function mb_strrichr($haystack, $needle, $part = false, $encoding = null)
463 $needle = self::mb_substr($needle, 0, 1, $encoding);
464 $pos = self::mb_strripos($haystack, $needle, $encoding);
466 return self::getSubpart($pos, $part, $haystack, $encoding);
469 public static function mb_strripos($haystack, $needle, $offset = 0, $encoding = null)
471 $haystack = self::mb_convert_case($haystack, self::MB_CASE_FOLD, $encoding);
472 $needle = self::mb_convert_case($needle, self::MB_CASE_FOLD, $encoding);
474 return self::mb_strrpos($haystack, $needle, $offset, $encoding);
477 public static function mb_strstr($haystack, $needle, $part = false, $encoding = null)
479 $pos = strpos($haystack, $needle);
480 if (false === $pos) {
484 return substr($haystack, 0, $pos);
487 return substr($haystack, $pos);
490 public static function mb_get_info($type = 'all')
493 'internal_encoding' => self::$internalEncoding,
494 'http_output' => 'pass',
495 'http_output_conv_mimetypes' => '^(text/|application/xhtml\+xml)',
496 'func_overload' => 0,
497 'func_overload_list' => 'no overload',
498 'mail_charset' => 'UTF-8',
499 'mail_header_encoding' => 'BASE64',
500 'mail_body_encoding' => 'BASE64',
501 'illegal_chars' => 0,
502 'encoding_translation' => 'Off',
503 'language' => self::$language,
504 'detect_order' => self::$encodingList,
505 'substitute_character' => 'none',
506 'strict_detection' => 'Off',
509 if ('all' === $type) {
512 if (isset($info[$type])) {
519 public static function mb_http_input($type = '')
524 public static function mb_http_output($encoding = null)
526 return null !== $encoding ? 'pass' === $encoding : 'pass';
529 public static function mb_strwidth($s, $encoding = null)
531 $encoding = self::getEncoding($encoding);
533 if ('UTF-8' !== $encoding) {
534 $s = iconv($encoding, 'UTF-8//IGNORE', $s);
537 $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);
539 return ($wide << 1) + iconv_strlen($s, 'UTF-8');
542 public static function mb_substr_count($haystack, $needle, $encoding = null)
544 return substr_count($haystack, $needle);
547 public static function mb_output_handler($contents, $status)
552 public static function mb_chr($code, $encoding = null)
554 if (0x80 > $code %= 0x200000) {
556 } elseif (0x800 > $code) {
557 $s = chr(0xC0 | $code >> 6).chr(0x80 | $code & 0x3F);
558 } elseif (0x10000 > $code) {
559 $s = chr(0xE0 | $code >> 12).chr(0x80 | $code >> 6 & 0x3F).chr(0x80 | $code & 0x3F);
561 $s = chr(0xF0 | $code >> 18).chr(0x80 | $code >> 12 & 0x3F).chr(0x80 | $code >> 6 & 0x3F).chr(0x80 | $code & 0x3F);
564 if ('UTF-8' !== $encoding = self::getEncoding($encoding)) {
565 $s = mb_convert_encoding($s, $encoding, 'UTF-8');
571 public static function mb_ord($s, $encoding = null)
573 if ('UTF-8' !== $encoding = self::getEncoding($encoding)) {
574 $s = mb_convert_encoding($s, 'UTF-8', $encoding);
577 $code = ($s = unpack('C*', substr($s, 0, 4))) ? $s[1] : 0;
579 return (($code - 0xF0) << 18) + (($s[2] - 0x80) << 12) + (($s[3] - 0x80) << 6) + $s[4] - 0x80;
582 return (($code - 0xE0) << 12) + (($s[2] - 0x80) << 6) + $s[3] - 0x80;
585 return (($code - 0xC0) << 6) + $s[2] - 0x80;
591 private static function getSubpart($pos, $part, $haystack, $encoding)
593 if (false === $pos) {
597 return self::mb_substr($haystack, 0, $pos, $encoding);
600 return self::mb_substr($haystack, $pos, null, $encoding);
603 private static function html_encoding_callback($m)
607 $m = unpack('C*', htmlentities($m[0], ENT_COMPAT, 'UTF-8'));
609 while (isset($m[$i])) {
611 $entities .= chr($m[$i++]);
614 if (0xF0 <= $m[$i]) {
615 $c = (($m[$i++] - 0xF0) << 18) + (($m[$i++] - 0x80) << 12) + (($m[$i++] - 0x80) << 6) + $m[$i++] - 0x80;
616 } elseif (0xE0 <= $m[$i]) {
617 $c = (($m[$i++] - 0xE0) << 12) + (($m[$i++] - 0x80) << 6) + $m[$i++] - 0x80;
619 $c = (($m[$i++] - 0xC0) << 6) + $m[$i++] - 0x80;
622 $entities .= '&#'.$c.';';
628 private static function title_case_lower($s)
630 return self::mb_convert_case($s[0], MB_CASE_LOWER, 'UTF-8');
633 private static function title_case_upper($s)
635 return self::mb_convert_case($s[0], MB_CASE_UPPER, 'UTF-8');
638 private static function getData($file)
640 if (file_exists($file = __DIR__.'/Resources/unidata/'.$file.'.php')) {
641 return require $file;
647 private static function getEncoding($encoding)
649 if (null === $encoding) {
650 return self::$internalEncoding;
653 $encoding = strtoupper($encoding);
655 if ('8BIT' === $encoding || 'BINARY' === $encoding) {
658 if ('UTF8' === $encoding) {