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\Iconv;
15 * iconv implementation in pure PHP, UTF-8 centric.
18 * - iconv - Convert string to requested character encoding
19 * - iconv_mime_decode - Decodes a MIME header field
20 * - iconv_mime_decode_headers - Decodes multiple MIME header fields at once
21 * - iconv_get_encoding - Retrieve internal configuration variables of iconv extension
22 * - iconv_set_encoding - Set current setting for character encoding conversion
23 * - iconv_mime_encode - Composes a MIME header field
24 * - iconv_strlen - Returns the character count of string
25 * - iconv_strpos - Finds position of first occurrence of a needle within a haystack
26 * - iconv_strrpos - Finds the last occurrence of a needle within a haystack
27 * - iconv_substr - Cut out part of a string
29 * Charsets available for conversion are defined by files
30 * in the charset/ directory and by Iconv::$alias below.
31 * You're welcome to send back any addition you make.
33 * @author Nicolas Grekas <p@tchwork.com>
39 const ERROR_ILLEGAL_CHARACTER = 'iconv(): Detected an illegal character in input string';
40 const ERROR_WRONG_CHARSET = 'iconv(): Wrong charset, conversion from `%s\' to `%s\' is not allowed';
42 public static $inputEncoding = 'utf-8';
43 public static $outputEncoding = 'utf-8';
44 public static $internalEncoding = 'utf-8';
46 private static $alias = array(
48 'ascii' => 'us-ascii',
49 'tis-620' => 'iso-8859-11',
50 'cp1250' => 'windows-1250',
51 'cp1251' => 'windows-1251',
52 'cp1252' => 'windows-1252',
53 'cp1253' => 'windows-1253',
54 'cp1254' => 'windows-1254',
55 'cp1255' => 'windows-1255',
56 'cp1256' => 'windows-1256',
57 'cp1257' => 'windows-1257',
58 'cp1258' => 'windows-1258',
59 'shift-jis' => 'cp932',
60 'shift_jis' => 'cp932',
61 'latin1' => 'iso-8859-1',
62 'latin2' => 'iso-8859-2',
63 'latin3' => 'iso-8859-3',
64 'latin4' => 'iso-8859-4',
65 'latin5' => 'iso-8859-9',
66 'latin6' => 'iso-8859-10',
67 'latin7' => 'iso-8859-13',
68 'latin8' => 'iso-8859-14',
69 'latin9' => 'iso-8859-15',
70 'latin10' => 'iso-8859-16',
71 'iso8859-1' => 'iso-8859-1',
72 'iso8859-2' => 'iso-8859-2',
73 'iso8859-3' => 'iso-8859-3',
74 'iso8859-4' => 'iso-8859-4',
75 'iso8859-5' => 'iso-8859-5',
76 'iso8859-6' => 'iso-8859-6',
77 'iso8859-7' => 'iso-8859-7',
78 'iso8859-8' => 'iso-8859-8',
79 'iso8859-9' => 'iso-8859-9',
80 'iso8859-10' => 'iso-8859-10',
81 'iso8859-11' => 'iso-8859-11',
82 'iso8859-12' => 'iso-8859-12',
83 'iso8859-13' => 'iso-8859-13',
84 'iso8859-14' => 'iso-8859-14',
85 'iso8859-15' => 'iso-8859-15',
86 'iso8859-16' => 'iso-8859-16',
87 'iso_8859-1' => 'iso-8859-1',
88 'iso_8859-2' => 'iso-8859-2',
89 'iso_8859-3' => 'iso-8859-3',
90 'iso_8859-4' => 'iso-8859-4',
91 'iso_8859-5' => 'iso-8859-5',
92 'iso_8859-6' => 'iso-8859-6',
93 'iso_8859-7' => 'iso-8859-7',
94 'iso_8859-8' => 'iso-8859-8',
95 'iso_8859-9' => 'iso-8859-9',
96 'iso_8859-10' => 'iso-8859-10',
97 'iso_8859-11' => 'iso-8859-11',
98 'iso_8859-12' => 'iso-8859-12',
99 'iso_8859-13' => 'iso-8859-13',
100 'iso_8859-14' => 'iso-8859-14',
101 'iso_8859-15' => 'iso-8859-15',
102 'iso_8859-16' => 'iso-8859-16',
103 'iso88591' => 'iso-8859-1',
104 'iso88592' => 'iso-8859-2',
105 'iso88593' => 'iso-8859-3',
106 'iso88594' => 'iso-8859-4',
107 'iso88595' => 'iso-8859-5',
108 'iso88596' => 'iso-8859-6',
109 'iso88597' => 'iso-8859-7',
110 'iso88598' => 'iso-8859-8',
111 'iso88599' => 'iso-8859-9',
112 'iso885910' => 'iso-8859-10',
113 'iso885911' => 'iso-8859-11',
114 'iso885912' => 'iso-8859-12',
115 'iso885913' => 'iso-8859-13',
116 'iso885914' => 'iso-8859-14',
117 'iso885915' => 'iso-8859-15',
118 'iso885916' => 'iso-8859-16',
120 private static $translitMap = array();
121 private static $convertMap = array();
122 private static $errorHandler;
123 private static $lastError;
125 private static $ulenMask = array("\xC0" => 2, "\xD0" => 2, "\xE0" => 3, "\xF0" => 4);
126 private static $isValidUtf8;
128 public static function iconv($inCharset, $outCharset, $str)
130 $str = (string) $str;
135 // Prepare for //IGNORE and //TRANSLIT
137 $translit = $ignore = '';
139 $outCharset = strtolower($outCharset);
140 $inCharset = strtolower($inCharset);
142 if ('' === $outCharset) {
143 $outCharset = 'iso-8859-1';
145 if ('' === $inCharset) {
146 $inCharset = 'iso-8859-1';
152 if ('//translit' === substr($outCharset, -10)) {
153 $loop = $translit = true;
154 $outCharset = substr($outCharset, 0, -10);
157 if ('//ignore' === substr($outCharset, -8)) {
158 $loop = $ignore = true;
159 $outCharset = substr($outCharset, 0, -8);
166 if ('//translit' === substr($inCharset, -10)) {
168 $inCharset = substr($inCharset, 0, -10);
171 if ('//ignore' === substr($inCharset, -8)) {
173 $inCharset = substr($inCharset, 0, -8);
177 if (isset(self::$alias[ $inCharset])) {
178 $inCharset = self::$alias[ $inCharset];
180 if (isset(self::$alias[$outCharset])) {
181 $outCharset = self::$alias[$outCharset];
186 if (('utf-8' !== $inCharset && !self::loadMap('from.', $inCharset, $inMap))
187 || ('utf-8' !== $outCharset && !self::loadMap('to.', $outCharset, $outMap))) {
188 trigger_error(sprintf(self::ERROR_WRONG_CHARSET, $inCharset, $outCharset));
193 if ('utf-8' !== $inCharset) {
194 // Convert input to UTF-8
196 if (self::mapToUtf8($result, $inMap, $str, $ignore)) {
201 self::$isValidUtf8 = true;
203 self::$isValidUtf8 = preg_match('//u', $str);
205 if (!self::$isValidUtf8 && !$ignore) {
206 trigger_error(self::ERROR_ILLEGAL_CHARACTER);
211 if ('utf-8' === $outCharset) {
213 $str = self::utf8ToUtf8($str, $ignore);
217 if ('utf-8' !== $outCharset && false !== $str) {
218 // Convert output to UTF-8
220 if (self::mapFromUtf8($result, $outMap, $str, $ignore, $translit)) {
230 public static function iconv_mime_decode_headers($str, $mode = 0, $charset = null)
232 if (null === $charset) {
233 $charset = self::$internalEncoding;
236 if (false !== strpos($str, "\r")) {
237 $str = strtr(str_replace("\r\n", "\n", $str), "\r", "\n");
239 $str = explode("\n\n", $str, 2);
243 $str = preg_split('/\n(?![ \t])/', $str[0]);
244 foreach ($str as $str) {
245 $str = self::iconv_mime_decode($str, $mode, $charset);
246 if (false === $str) {
249 $str = explode(':', $str, 2);
251 if (2 === count($str)) {
252 if (isset($headers[$str[0]])) {
253 if (!is_array($headers[$str[0]])) {
254 $headers[$str[0]] = array($headers[$str[0]]);
256 $headers[$str[0]][] = ltrim($str[1]);
258 $headers[$str[0]] = ltrim($str[1]);
266 public static function iconv_mime_decode($str, $mode = 0, $charset = null)
268 if (null === $charset) {
269 $charset = self::$internalEncoding;
271 if (ICONV_MIME_DECODE_CONTINUE_ON_ERROR & $mode) {
272 $charset .= '//IGNORE';
275 if (false !== strpos($str, "\r")) {
276 $str = strtr(str_replace("\r\n", "\n", $str), "\r", "\n");
278 $str = preg_split('/\n(?![ \t])/', rtrim($str), 2);
279 $str = preg_replace('/[ \t]*\n[ \t]+/', ' ', rtrim($str[0]));
280 $str = preg_split('/=\?([^?]+)\?([bqBQ])\?(.*?)\?=/', $str, -1, PREG_SPLIT_DELIM_CAPTURE);
282 $result = self::iconv('utf-8', $charset, $str[0]);
283 if (false === $result) {
291 $c = strtolower($str[$i]);
292 if ((ICONV_MIME_DECODE_CONTINUE_ON_ERROR & $mode)
294 && !isset(self::$alias[$c])
295 && !self::loadMap('from.', $c, $d)) {
297 } elseif ('B' === strtoupper($str[$i + 1])) {
298 $d = base64_decode($str[$i + 2]);
300 $d = rawurldecode(strtr(str_replace('%', '%25', $str[$i + 2]), '=_', '% '));
305 if ('' === $d = self::iconv($c, $charset, $d)) {
306 $str[$i + 3] = substr($str[$i + 3], 1);
311 $d = self::iconv('utf-8', $charset, $str[$i + 3]);
312 if ('' !== trim($d)) {
315 } elseif (ICONV_MIME_DECODE_CONTINUE_ON_ERROR & $mode) {
316 $result .= "=?{$str[$i]}?{$str[$i + 1]}?{$str[$i + 2]}?={$str[$i + 3]}";
328 public static function iconv_get_encoding($type = 'all')
331 case 'input_encoding': return self::$inputEncoding;
332 case 'output_encoding': return self::$outputEncoding;
333 case 'internal_encoding': return self::$internalEncoding;
337 'input_encoding' => self::$inputEncoding,
338 'output_encoding' => self::$outputEncoding,
339 'internal_encoding' => self::$internalEncoding,
343 public static function iconv_set_encoding($type, $charset)
346 case 'input_encoding': self::$inputEncoding = $charset; break;
347 case 'output_encoding': self::$outputEncoding = $charset; break;
348 case 'internal_encoding': self::$internalEncoding = $charset; break;
350 default: return false;
356 public static function iconv_mime_encode($fieldName, $fieldValue, $pref = null)
358 if (!is_array($pref)) {
364 'input-charset' => self::$internalEncoding,
365 'output-charset' => self::$internalEncoding,
367 'line-break-chars' => "\r\n",
370 if (preg_match('/[\x80-\xFF]/', $fieldName)) {
374 $scheme = strtoupper(substr($pref['scheme'], 0, 1));
375 $in = strtolower($pref['input-charset']);
376 $out = strtolower($pref['output-charset']);
378 if ('utf-8' !== $in && false === $fieldValue = self::iconv($in, 'utf-8', $fieldValue)) {
382 preg_match_all('/./us', $fieldValue, $chars);
384 $chars = isset($chars[0]) ? $chars[0] : array();
386 $lineBreak = (int) $pref['line-length'];
387 $lineStart = "=?{$pref['output-charset']}?{$scheme}?";
388 $lineLength = strlen($fieldName) + 2 + strlen($lineStart) + 2;
389 $lineOffset = strlen($lineStart) + 3;
392 $fieldValue = array();
394 $Q = 'Q' === $scheme;
396 foreach ($chars as $c) {
397 if ('utf-8' !== $out && false === $c = self::iconv('utf-8', $out, $c)) {
402 ? $c = preg_replace_callback(
403 '/[=_\?\x00-\x1F\x80-\xFF]/',
404 array(__CLASS__, 'qpByteCallback'),
407 : base64_encode($lineData.$c);
409 if (isset($o[$lineBreak - $lineLength])) {
411 $lineData = base64_encode($lineData);
413 $fieldValue[] = $lineStart.$lineData.'?=';
414 $lineLength = $lineOffset;
419 $Q && $lineLength += strlen($c);
422 if ('' !== $lineData) {
424 $lineData = base64_encode($lineData);
426 $fieldValue[] = $lineStart.$lineData.'?=';
429 return $fieldName.': '.implode($pref['line-break-chars'].' ', $fieldValue);
432 public static function iconv_strlen($s, $encoding = null)
434 static $hasXml = null;
435 if (null === $hasXml) {
436 $hasXml = extension_loaded('xml');
440 return self::strlen1($s, $encoding);
443 return self::strlen2($s, $encoding);
446 public static function strlen1($s, $encoding = null)
448 if (null === $encoding) {
449 $encoding = self::$internalEncoding;
451 if (0 !== stripos($encoding, 'utf-8') && false === $s = self::iconv($encoding, 'utf-8', $s)) {
455 return strlen(utf8_decode($s));
458 public static function strlen2($s, $encoding = null)
460 if (null === $encoding) {
461 $encoding = self::$internalEncoding;
463 if (0 !== stripos($encoding, 'utf-8') && false === $s = self::iconv($encoding, 'utf-8', $s)) {
467 $ulenMask = self::$ulenMask;
474 $u = $s[$i] & "\xF0";
475 $i += isset($ulenMask[$u]) ? $ulenMask[$u] : 1;
482 public static function iconv_strpos($haystack, $needle, $offset = 0, $encoding = null)
484 if (null === $encoding) {
485 $encoding = self::$internalEncoding;
488 if (0 !== stripos($encoding, 'utf-8')) {
489 if (false === $haystack = self::iconv($encoding, 'utf-8', $haystack)) {
492 if (false === $needle = self::iconv($encoding, 'utf-8', $needle)) {
497 if ($offset = (int) $offset) {
498 $haystack = self::iconv_substr($haystack, $offset, 2147483647, 'utf-8');
500 $pos = strpos($haystack, $needle);
502 return false === $pos ? false : ($offset + ($pos ? self::iconv_strlen(substr($haystack, 0, $pos), 'utf-8') : 0));
505 public static function iconv_strrpos($haystack, $needle, $encoding = null)
507 if (null === $encoding) {
508 $encoding = self::$internalEncoding;
511 if (0 !== stripos($encoding, 'utf-8')) {
512 if (false === $haystack = self::iconv($encoding, 'utf-8', $haystack)) {
515 if (false === $needle = self::iconv($encoding, 'utf-8', $needle)) {
520 $pos = isset($needle[0]) ? strrpos($haystack, $needle) : false;
522 return false === $pos ? false : self::iconv_strlen($pos ? substr($haystack, 0, $pos) : $haystack, 'utf-8');
525 public static function iconv_substr($s, $start, $length = 2147483647, $encoding = null)
527 if (null === $encoding) {
528 $encoding = self::$internalEncoding;
530 if (0 !== stripos($encoding, 'utf-8')) {
532 } elseif (false === $s = self::iconv($encoding, 'utf-8', $s)) {
537 $slen = self::iconv_strlen($s, 'utf-8');
538 $start = (int) $start;
546 if ($start >= $slen) {
550 $rx = $slen - $start;
566 $rx = '/^'.($start ? self::pregOffset($start) : '').'('.self::pregOffset($length).')/u';
568 $s = preg_match($rx, $s, $s) ? $s[1] : '';
570 if (null === $encoding) {
574 return self::iconv('utf-8', $encoding, $s);
577 private static function loadMap($type, $charset, &$map)
579 if (!isset(self::$convertMap[$type.$charset])) {
580 if (false === $map = self::getData($type.$charset)) {
581 if ('to.' === $type && self::loadMap('from.', $charset, $map)) {
582 $map = array_flip($map);
588 self::$convertMap[$type.$charset] = $map;
590 $map = self::$convertMap[$type.$charset];
596 private static function utf8ToUtf8($str, $ignore)
598 $ulenMask = self::$ulenMask;
599 $valid = self::$isValidUtf8;
606 if ($str[$i] < "\x80") {
607 $u[$j++] = $str[$i++];
609 $ulen = $str[$i] & "\xF0";
610 $ulen = isset($ulenMask[$ulen]) ? $ulenMask[$ulen] : 1;
611 $uchr = substr($str, $i, $ulen);
613 if (1 === $ulen || !($valid || preg_match('/^.$/us', $uchr))) {
619 trigger_error(self::ERROR_ILLEGAL_CHARACTER);
628 isset($uchr[1]) && 0 !== ($u[$j++] = $uchr[1])
629 && isset($uchr[2]) && 0 !== ($u[$j++] = $uchr[2])
630 && isset($uchr[3]) && 0 !== ($u[$j++] = $uchr[3]);
634 return substr($u, 0, $j);
637 private static function mapToUtf8(&$result, array $map, $str, $ignore)
640 for ($i = 0; $i < $len; ++$i) {
641 if (isset($str[$i + 1], $map[$str[$i].$str[$i + 1]])) {
642 $result .= $map[$str[$i].$str[++$i]];
643 } elseif (isset($map[$str[$i]])) {
644 $result .= $map[$str[$i]];
645 } elseif (!$ignore) {
646 trigger_error(self::ERROR_ILLEGAL_CHARACTER);
655 private static function mapFromUtf8(&$result, array $map, $str, $ignore, $translit)
657 $ulenMask = self::$ulenMask;
658 $valid = self::$isValidUtf8;
660 if ($translit && !self::$translitMap) {
661 self::$translitMap = self::getData('translit');
668 if ($str[$i] < "\x80") {
671 $ulen = $str[$i] & "\xF0";
672 $ulen = isset($ulenMask[$ulen]) ? $ulenMask[$ulen] : 1;
673 $uchr = substr($str, $i, $ulen);
675 if ($ignore && (1 === $ulen || !($valid || preg_match('/^.$/us', $uchr)))) {
683 if (isset($map[$uchr])) {
684 $result .= $map[$uchr];
685 } elseif ($translit) {
686 if (isset(self::$translitMap[$uchr])) {
687 $uchr = self::$translitMap[$uchr];
688 } elseif ($uchr >= "\xC3\x80") {
689 $uchr = \Normalizer::normalize($uchr, \Normalizer::NFD);
691 if ($uchr[0] < "\x80") {
700 $str = $uchr.substr($str, $i);
703 } elseif (!$ignore) {
711 private static function qpByteCallback(array $m)
713 return '='.strtoupper(dechex(ord($m[0])));
716 private static function pregOffset($offset)
719 $offset = (int) $offset;
721 while ($offset > 65535) {
726 return implode('', $rx).'.{'.$offset.'}';
729 private static function getData($file)
731 if (file_exists($file = __DIR__.'/Resources/charset/'.$file.'.php')) {
732 return require $file;