‪TYPO3CMS  9.5
EncodingHelper.php
Go to the documentation of this file.
1 <?php
9 
11 {
21  public static function ‪toUtf8($string = '', $encoding = 'iso-8859-1', $safe_mode = false)
22  {
23  $safe = ($safe_mode) ? $string : false;
24  if (strtoupper($encoding) == 'UTF-8' || strtoupper($encoding) == 'UTF8') {
25  return $string;
26  }
27  if (strtoupper($encoding) == 'ISO-8859-1') {
28  return \utf8_encode($string);
29  }
30  if (strtoupper($encoding) == 'WINDOWS-1252') {
31  return \utf8_encode(self::map_w1252_iso8859_1($string));
32  }
33 
34  if (strtoupper($encoding) == 'UNICODE-1-1-UTF-7') {
35  $encoding = 'utf-7';
36  }
37  if (function_exists('mb_convert_encoding')) {
38  $conv = @mb_convert_encoding($string, 'UTF-8', strtoupper($encoding));
39  if ($conv) {
40  return $conv;
41  }
42  }
43  if (function_exists('iconv')) {
44  $conv = @iconv(strtoupper($encoding), 'UTF-8', $string);
45  if ($conv) {
46  return $conv;
47  }
48  }
49  if (function_exists('libiconv')) {
50  $conv = @libiconv(strtoupper($encoding), 'UTF-8', $string);
51  if ($conv) {
52  return $conv;
53  }
54  }
55 
56  return $safe;
57  }
58 
68  public static function ‪fromUtf8($string = '', $encoding = 'iso-8859-1', $safe_mode = false)
69  {
70  $safe = ($safe_mode) ? $string : false;
71  if (!$encoding) {
72  $encoding = 'ISO-8859-1';
73  }
74  if (strtoupper($encoding) == 'UTF-8' || strtoupper($encoding) == 'UTF8') {
75  return $string;
76  }
77  if (strtoupper($encoding) == 'ISO-8859-1') {
78  return utf8_decode($string);
79  }
80  if (strtoupper($encoding) == 'WINDOWS-1252') {
81  return ‪self::map_iso8859_1_w1252(utf8_decode($string));
82  }
83 
84  if (strtoupper($encoding) == 'UNICODE-1-1-UTF-7') {
85  $encoding = 'utf-7';
86  }
87  if (function_exists('mb_convert_encoding')) {
88  $conv = @mb_convert_encoding($string, strtoupper($encoding), 'UTF-8');
89  if ($conv) {
90  return $conv;
91  }
92  }
93  if (function_exists('iconv')) {
94  $conv = @iconv('UTF-8', strtoupper($encoding), $string);
95  if ($conv) {
96  return $conv;
97  }
98  }
99  if (function_exists('libiconv')) {
100  $conv = @libiconv('UTF-8', strtoupper($encoding), $string);
101  if ($conv) {
102  return $conv;
103  }
104  }
105 
106  return $safe;
107  }
108 
117  protected static function ‪map_w1252_iso8859_1($string = '')
118  {
119  if ($string == '') {
120  return '';
121  }
122  $return = '';
123 
124  for ($i = 0; $i < strlen($string); ++$i) {
125  $c = ord($string{$i});
126  switch ($c) {
127  case 129: $return .= chr(252); break;
128  case 132: $return .= chr(228); break;
129  case 142: $return .= chr(196); break;
130  case 148: $return .= chr(246); break;
131  case 153: $return .= chr(214); break;
132  case 154: $return .= chr(220); break;
133  case 225: $return .= chr(223); break;
134  default: $return .= chr($c);
135  }
136  }
137 
138  return $return;
139  }
140 
149  protected static function ‪map_iso8859_1_w1252($string = '')
150  {
151  if ($string == '') {
152  return '';
153  }
154 
155  $return = '';
156  for ($i = 0; $i < strlen($string); ++$i) {
157  $c = ord($string{$i});
158  switch ($c) {
159  case 196: $return .= chr(142); break;
160  case 214: $return .= chr(153); break;
161  case 220: $return .= chr(154); break;
162  case 223: $return .= chr(225); break;
163  case 228: $return .= chr(132); break;
164  case 246: $return .= chr(148); break;
165  case 252: $return .= chr(129); break;
166  default: $return .= chr($c);
167  }
168  }
169 
170  return $return;
171  }
172 }
‪Mso\IdnaConvert\EncodingHelper\map_w1252_iso8859_1
‪static string map_w1252_iso8859_1($string='')
Definition: EncodingHelper.php:117
‪Mso\IdnaConvert\EncodingHelper\map_iso8859_1_w1252
‪static string map_iso8859_1_w1252($string='')
Definition: EncodingHelper.php:149
‪Mso\IdnaConvert\EncodingHelper\fromUtf8
‪static string false fromUtf8($string='', $encoding='iso-8859-1', $safe_mode=false)
Definition: EncodingHelper.php:68
‪Mso\IdnaConvert\EncodingHelper\toUtf8
‪static string false toUtf8($string='', $encoding='iso-8859-1', $safe_mode=false)
Definition: EncodingHelper.php:21
‪Mso\IdnaConvert
Definition: EncodingHelper.php:8
‪Mso\IdnaConvert\EncodingHelper
Definition: EncodingHelper.php:11