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",
'ι'),
81 if (\is_array($fromEncoding) ||
false !== strpos($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) {
128 return $ok ? $fromEncoding :
false;
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);
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);
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) {
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]) {
194 if (
null === $encoding) {
198 return iconv(
'UTF-8', $encoding.
'//IGNORE', $s);
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);
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);
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);
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);
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;
368 if (
null === $lang) {
369 return self::$language;
372 switch ($lang = strtolower($lang)) {
375 self::$language = $lang;
385 return array(
'UTF-8');
390 switch (strtoupper($encoding)) {
393 return array(
'utf8');
401 if (
null === $encoding) {
405 $encoding = self::$internalEncoding;
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)) {
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;
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) {
517 $haystack =
self::mb_substr($haystack, $offset, 2147483647, $encoding);
521 $pos = iconv_strrpos($haystack, $needle, $encoding);
523 return false !== $pos ? $offset + $pos :
false;
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)
579 public static function mb_stristr($haystack, $needle, $part =
false, $encoding =
null)
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);
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)
603 return self::getSubpart($pos, $part, $haystack, $encoding);
606 public static function mb_strripos($haystack, $needle, $offset = 0, $encoding =
null)
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);
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])) {
663 return null !== $encoding ?
'pass' === $encoding :
'pass';
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');
681 return substr_count($haystack, $needle);
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)) {
708 public static function mb_ord($s, $encoding =
null)
710 if (
'UTF-8' !== $encoding = self::getEncoding($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) {
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) {