1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
encoding.php
См. документацию.
1<?php
2
3namespace Bitrix\Main\Text;
4
5use Bitrix\Main\Config\Configuration;
6
8{
17 public static function convertEncoding($data, $charsetFrom, $charsetTo)
18 {
19 $charsetFrom = static::resolveAlias($charsetFrom);
20 $charsetTo = static::resolveAlias($charsetTo);
21
22 if ((string)$charsetFrom == '' || (string)$charsetTo == '')
23 {
24 // incorrect encoding
25 return $data;
26 }
27
28 if (strcasecmp($charsetFrom, $charsetTo) == 0)
29 {
30 // no need to convert
31 return $data;
32 }
33
34 try
35 {
36 // mb_encoding_aliases emits an E_WARNING level error if encoding is unknown
37 if (@mb_encoding_aliases($charsetFrom) === false || @mb_encoding_aliases($charsetTo) === false)
38 {
39 // unknown encoding
40 return $data;
41 }
42 }
43 catch (\ValueError)
44 {
45 return $data;
46 }
47
48 return self::convert($data, $charsetFrom, $charsetTo);
49 }
50
51 protected static function resolveAlias($alias)
52 {
53 static $map = [
54 'csksc56011987' => 'euc-kr',
55 'ks_c_5601-1987' => 'euc-kr',
56 'ks_c_5601-1989' => 'euc-kr',
57 'ksc5601' => 'euc-kr',
58 'ksc_5601' => 'euc-kr',
59 'windows-1257' => 'ISO-8859-13',
60 ];
61
62 if (is_string($alias))
63 {
64 $alias = strtolower(trim($alias));
65 if (isset($map[$alias]))
66 {
67 return $map[$alias];
68 }
69 }
70
71 return $alias;
72 }
73
74 protected static function convert($data, $charsetFrom, $charsetTo)
75 {
76 if (is_array($data) || $data instanceof \SplFixedArray)
77 {
78 //let's do a recursion
79 if ($data instanceof \SplFixedArray)
80 {
81 $result = clone $data;
82 }
83 else
84 {
85 $result = [];
86 }
87 foreach ($data as $key => $value)
88 {
89 $newKey = self::convert($key, $charsetFrom, $charsetTo);
90 $newValue = self::convert($value, $charsetFrom, $charsetTo);
91
92 $result[$newKey] = $newValue;
93 }
94 return $result;
95 }
96 elseif (is_string($data))
97 {
98 if ($data == '')
99 {
100 return '';
101 }
102 return static::convertByMbstring($data, $charsetFrom, $charsetTo);
103 }
104 return $data;
105 }
106
114 public static function convertEncodingArray($data, $charsetFrom, $charsetTo)
115 {
116 return self::convertEncoding($data, $charsetFrom, $charsetTo);
117 }
118
123 public static function convertEncodingToCurrent($string)
124 {
125 return self::convertToUtf($string);
126 }
127
132 public static function convertToUtf($string)
133 {
134 if (self::detectUtf8($string))
135 {
136 return $string;
137 }
138
139 $from = Configuration::getValue("default_charset");
140
141 if (!$from)
142 {
143 $from = defined('BX_DEFAULT_CHARSET') ? constant('BX_DEFAULT_CHARSET') : 'Windows-1251';
144 }
145
146 return self::convertEncoding($string, $from, 'UTF-8');
147 }
148
154 public static function detectUtf8($string, $replaceHex = true)
155 {
156 if ($replaceHex)
157 {
158 $string = preg_replace_callback(
159 "/(%)([\\dA-F]{2})/i",
160 function ($match) {
161 return chr(hexdec($match[2]));
162 },
163 $string
164 );
165 }
166
167 return (bool)preg_match('//u', $string);
168 }
169
170 protected static function convertByMbstring($data, $charsetFrom, $charsetTo)
171 {
172 //For UTF-16 we have to detect the order of bytes
173 //Default for mbstring extension is Big endian
174 //Little endian have to pointed explicitly
175 if (strtoupper($charsetFrom) == "UTF-16")
176 {
177 $ch = substr($data, 0, 1);
178 if ($ch == "\xFF" && substr($data, 1, 1) == "\xFE")
179 {
180 //If Little endian found - cutoff BOF bytes and point mbstring to this fact explicitly
181 $res = mb_convert_encoding(substr($data, 2), $charsetTo, "UTF-16LE");
182 }
183 elseif ($ch == "\xFE" && substr($data, 1, 1) == "\xFF")
184 {
185 //If it is Big endian, just remove BOF bytes
186 $res = mb_convert_encoding(substr($data, 2), $charsetTo, $charsetFrom);
187 }
188 else
189 {
190 //Otherwise, assime Little endian without BOF
191 $res = mb_convert_encoding($data, $charsetTo, "UTF-16LE");
192 }
193 }
194 else
195 {
196 $res = mb_convert_encoding($data, $charsetTo, $charsetFrom);
197 }
198
199 return $res;
200 }
201}
static convertEncoding($data, $charsetFrom, $charsetTo)
Определения encoding.php:17
static resolveAlias($alias)
Определения encoding.php:51
static convertToUtf($string)
Определения encoding.php:132
static convertByMbstring($data, $charsetFrom, $charsetTo)
Определения encoding.php:170
static convertEncodingArray($data, $charsetFrom, $charsetTo)
Определения encoding.php:114
static convertEncodingToCurrent($string)
Определения encoding.php:123
static detectUtf8($string, $replaceHex=true)
Определения encoding.php:154
static convert($data, $charsetFrom, $charsetTo)
Определения encoding.php:74
$data['IS_AVAILABLE']
Определения .description.php:13
$res
Определения filter_act.php:7
$result
Определения get_property_values.php:14
else $ch
Определения group_list_element_edit.php:27
$map
Определения config.php:5
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
if(empty($signedUserToken)) $key
Определения quickway.php:257